-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp008.cpp
48 lines (48 loc) · 1.34 KB
/
p008.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
class Solution {
public:
int myAtoi(string str) {
long long result = 0;
int i;
for (i = 0; i < str.length(); i++)
{
if (str[i] == ' ') continue;
else if (str[i] == '-' || str[i] == '+' || (str[i] >= '0' && str[i] <= '9')) break;
else return 0;
}
if (i == str.length()) return 0;
if (str[i] == '-')
{
i++;
for (; i < str.length(); i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
result = (result<<3)+(result<<1)-(str[i]-'0');
if (result < -2147483648) return -2147483648;
}
else
{
return result;
}
}
return result;
}
else
{
if (str[i] == '+') i++;
for (; i < str.length(); i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
result = (result<<3)+(result<<1)+(str[i]-'0');
if (result > 2147483647) return 2147483647;
}
else
{
return result;
}
}
return result;
}
}
};