-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path8. String to Integer (atoi).cpp
41 lines (37 loc) · 1.3 KB
/
8. 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
35
36
37
38
39
40
41
class Solution {
public:
int myAtoi(string str) {
char sign = '+';
int index = -1;
for(int i = 0; i < str.length(); ++i) {
if(str[i] != '-' && str[i] != '+' && str[i] != ' ' && !isdigit(str[i]))
return 0;
if(str[i] == '-')
sign = '-';
if(str[i] == '-' && i + 1 < str.length() && !isdigit(str[i + 1]))
return 0;
if(str[i] == '+')
sign = '+';
if(str[i] == '+' && i + 1 < str.length() && !isdigit(str[i + 1]))
return 0;
if(isdigit(str[i])) {
index = i;
break;
}
}
if(index == -1)
return 0;
int solution = 0;
for(int i = index; isdigit(str[i]) && i < str.length(); ++i) {
if(solution > INT_MAX / 10 || (solution == INT_MAX / 10 && str[i] - '0' > 7))
return INT_MAX;
if(solution < INT_MIN / 10 || (solution == INT_MIN / 10 && str[i] - '0' > 8))
return INT_MIN;
if(sign == '-')
solution = solution * 10 + -(str[i] - '0');
else
solution = solution * 10 + (str[i] - '0');
}
return solution;
}
};