forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0008.cpp
More file actions
42 lines (32 loc) · 824 Bytes
/
0008.cpp
File metadata and controls
42 lines (32 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public:
int myAtoi(string s) {
int len = s.length();
int i = 0;
if(len == 0)
return 0;
for(i = 0; i < len && s[i] == ' '; i++);
if(i == len)
return 0;
int sin = 1;
if(s[i] == '-'){
sin = -1;
i = i + 1;
}
else if(s[i] == '+'){
i = i + 1;
}
long ans = 0;
while(i < len && ans < INT_MAX && isdigit(s[i])){
ans = ans * 10 + (s[i] - '0');
i = i + 1;
}
if(ans > INT_MAX){
if(sin == 1)
return INT_MAX;
else
return INT_MIN;
}
return ans * sin;
}
};