数据超过时要及时返回,不然容易出错
class Solution {
public:
int strToInt(string str) {
int i=0;
long long res=0;
while(str[i]==' ')i++;
int rt=1;
if(str[i]=='+'){i++;}
else if(str[i]=='-'){rt=-rt;i++;}
while(str[i]>='0'&&str[i]<='9'){
res=res*10+str[i]-'0';
i++;
if(res*rt>INT_MAX)return INT_MAX;
if(res*rt<INT_MIN)return INT_MIN;
}
return res*rt;
}
};