-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.txt
40 lines (28 loc) · 858 Bytes
/
8.txt
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
class Solution
{
public:
int myAtoi(string str)
{
if (str.empty()) return 0;
long long base = 0 ;
int sign = 1;
int i = 0;
while(str[i] == ' ') i++;
if(str[i] == '+' || str[i] == '-')//·ÒëÕý¸ººÅ
{
sign = (str[i] == '-')? -1: 1;
i++;
}
while(str[i] >= '0' && str[i] <= '9' && i < str.size())
{
int digit = str[i] - '0';
base=base * 10 + digit;
if ((sign == 1 && base > INT_MAX) ||(sign == -1 && -base < INT_MIN))
{
return (sign == 1)? INT_MAX:INT_MIN;
}
i++;
}
return base*sign;
}
};