-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.cpp
31 lines (31 loc) · 906 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
class Solution {
public:
int myAtoi(string str) {
// - 45
// 0-9 48-57
// 0x7fffffff pow(2,31)-1
long long a=0;
int symbol=0;
int num=0;
bool minus=false;
for(int i=0;i<str.size();i++){
if(int(str[i])>=48&&int(str[i])<=57){
num++;
a=a*10+int(str[i]-48);
if(a>=0x7fffffff&&!minus){return INT_MAX;}
if(a>0x7fffffff&&minus){return INT_MIN;}
}else if(int(str[i])==45){
if(symbol||num>0){break;}
minus=true;
symbol=1;
}else if(int(str[i])==43){
if(symbol||num>0){break;}
minus=false;
symbol=1;
}else if(int(str[i])==32){
if(num>0||symbol){break;}
}else{break;}
}
return minus?-a:a;
}
};