forked from randlezhang/Leetcode-codewar-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyAtoi.cpp
46 lines (46 loc) · 1.38 KB
/
myAtoi.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
/* int myAtoi(string str) {
string num("-+0123456789");
string nums("0123456789");
long int atio=0;
int flag=0;
auto pos = str.find_first_of(num);
for(int i=0;i<pos;++i) {
if(str[i]!=' ') return 0;
}
if((str[pos]=='-')||(str[pos]=='+')) {if(str[pos]=='-') flag=1;++pos;}
auto npos=str.find_first_not_of(nums,pos);
if(npos==(-1)) npos=str.size();
for(int j=pos;j<npos;++j){
atio=atio*10+(str[j]-'0');
if(atio>INT_MAX) return (flag)? INT_MIN:INT_MAX;
}
atio=(flag)? -atio:atio;
if(atio<INT_MIN) return INT_MIN;
if(atio>INT_MAX) return INT_MAX;
return atio;
} */
int myAtoi(string str) {
long int atio=0;
int flag = 0;
int state=0;
for(int i=0;i<str.size();++i){
if(state==0){
if(str[i]==' ') continue;
if(str[i]=='-'){flag=1;state=1;continue;}
if(str[i]=='+'){state=1;continue;}
if((str[i]>='0')&&(str[i]<='9')) {state=1;atio=10*atio+str[i]-'0';continue;}
return 0;
}
if(state==1){
if((str[i]>='0')&&(str[i]<='9')) {
atio=10*atio+str[i]-'0';
if(atio>INT_MAX) return (flag)? INT_MIN:INT_MAX;
continue;}
break;
}
}
atio=(flag)? -atio:atio;
if(atio<INT_MIN) return INT_MIN;
if(atio>INT_MAX) return INT_MAX;
return atio;
}