-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0008-string-to-integer-atoi.cpp
34 lines (33 loc) · 1.08 KB
/
0008-string-to-integer-atoi.cpp
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
class Solution {
public:
int myAtoi(string s) {
bool flag = 0;
bool sign = 0;
long long ans = 0;
int digitIndex = 0;
for(int i=0; i<s.length(); i++){
if(s[i] == ' ')continue;
else if(s[i] == '+' && sign == 0){
if(i< s.length()-1 && (s[i+1] < '0' || s[i+1] > '9')) break;
sign = 1;
}
else if(s[i] == '-' && sign == 0){
flag = 1;
if(i< s.length()-1 && (s[i+1] < '0' || s[i+1] > '9')) break;
sign = 1;
}
else if(s[i] >= '0' && s[i] <= '9'){
int value = s[i]-'0';
if((ans)/10 > INT_MAX){
return flag == 1 ? INT_MIN : INT_MAX;
}
ans = (ans*10) + value;
if(i< s.length()-1 && (s[i+1] < '0' || s[i+1] > '9')) break;
}else break;
}
if(flag == 1) ans = -ans;
if(ans > INT_MAX) return INT_MAX;
else if(ans < INT_MIN) return INT_MIN;
return (int)ans;
}
};