-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path08_StringtoInteger.cpp
54 lines (47 loc) · 1.07 KB
/
08_StringtoInteger.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
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* @Author: [email protected]
* @Last Modified time: 2016-08-21 09:10:58
*/
class Solution {
public:
int myAtoi(string str){
/*
We need to handle four cases:
1. discards all leading whitespaces
2. sign of the number
3. overflow
4. invalid input
*/
const int len = str.size();
int i=0;
// Skip starting whitespace as many as possible.
while (str[i]==' ' && i<len) i++;
int sign = 1;
// Read plus or minus sign.
if(str[i] == '+') i++;
else if(str[i] == '-'){
i++;
sign = -1;
}
int num = 0;
for(; i<len; i++){
if(str[i] < '0' || str[i] > '9'){
break;
}
if(num > INT_MAX/10 || (num == INT_MAX / 10 && (str[i]-'0') > INT_MAX % 10)){
return sign == -1 ? INT_MIN : INT_MAX;
}
num = num * 10 + str[i] - '0';
}
return num * sign;
}
};
/*
""
" 12a"
" a12"
" +12"
" +-12"
"2147483648"
"-2147483648"
*/