-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.cpp
42 lines (34 loc) · 767 Bytes
/
8.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
class Solution {
public:
int myAtoi(string str) {
long ret = 0;
bool minus = false;
int i = 0;
while(isspace(str[i]))
{
++i;
}
if(str[i] == '+' || str[i] == '-')
{
if(str[i] == '-')
{
minus = true;
}
++i;
}
while(isdigit(str[i]) && str[i] != '\0')
{
ret = ret * 10 + str[i] - '0';
++i;
if(ret > INT_MAX)
{
return minus ? INT_MIN : INT_MAX;
}
}
if(minus)
{
ret *= -1;
}
return (int)ret;
}
};