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