8.字符转整数atoi
https://leetcode-cn.com/problems/string-to-integer-atoi/
class Solution {
public:
int myAtoi(string str) {
int i=0,j=0,res=0,flag=1,tmp;
while(i<str.size()&&str[i]==' ')i++;
if(i==str.size()) return 0;
else{
if(str[i]=='-'){
flag=-1;
i++;
}
else if(str[i]=='+'){
flag=1;
i++;
}
if(i<str.size()){
while(i<str.size()&&(str[i]>='0'&&str[i]<='9')){
tmp=str[i]-'0';
if( res>INT_MAX/10||(res==INT_MAX/10&&tmp>7)){
return INT_MAX;
}
if(res<INT_MIN/10||(res==INT_MIN/10&&tmp>8) ){
return INT_MIN;
}
res=res*10+flag*tmp;
i++;
}
return res;
}
else return 0;
}
}
};