0%
题目描述
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
| class Solution { public: int myAtoi(string str) { int i; int res=0; int t; int key=0; int w=0; for(i=0;str[i]==' '&&i<str.length();i++) {;} if(str[i]!='+'&&str[i]!='-'&&(!('0'<=str[i]&&str[i]<='9'))||i==str.length()) return 0; if(str[i]=='-') { key=1; i++; } else if(str[i]=='+') i++; for(;i<str.length();i++) { if('0'<=str[i]&&str[i]<='9') { t=str[i]-'0'; if((res>INT_MAX/10||(res==INT_MAX/10&&t>7))&&key==0) return INT_MAX; if((res>INT_MAX/10||(res==INT_MAX/10&&t>8))&&key==1) return INT_MIN; if(res==INT_MAX/10&&key==1) { res=-res*10-t; w=1; } else res=res*10+t; } else break; } if(key==1&&w==0) res=-res; return res; } };
|