-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.cpp
More file actions
30 lines (30 loc) · 735 Bytes
/
Copy path8.cpp
File metadata and controls
30 lines (30 loc) · 735 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
// simple.cpp
class Solution {
public:
int myAtoi(string str) {
int len = str.size();
if (len == 0)
return 0;
int i, j;
for (j = 0; j < len && str[j] == ' '; ++j)
;
int sign = 1;
if (str[j] == '-' || str[j] == '+') {
sign = str[j] == '-' ? -1 : 1;
++j;
}
if (!(str[j] >= '0' && str[j] <= '9'))
return 0;
for (i = j; i < len; ++i)
if (!(str[i] >= '0' && str[i] <= '9'))
break;
int result = 0;
while (j < i) {
result = result * 10 + str[j++] - '0';
if ((result > INT_MAX / 10 && j < i) ||
(result == INT_MAX / 10 && j < i && str[j] > '7'))
return sign == 1 ? INT_MAX : INT_MIN;
}
return sign * result;
}
};