这好像是阉割版分,以前做过一道各种字符都有,- -23这种类似的也需要考虑
class Solution {
public:
int strToInt(string str) {
int index = 0, len = str.length();
double ans = 0;
string buff = "";
bool flag = false;
while(index < len && str[index] == ' ') index++;
if (str[index] == '-') {index++; flag = true;}
else if (str[index] == '+') index++;
if(str[index] >= '0' && str[index] <= '9') {
while(str[index] >= '0' && str[index] <= '9') buff += str[index++];
}
for (int i = 0; i < buff.length(); ++i) {
ans += (buff[i] - '0') * pow(10, buff.length() - i - 1);
}
if (flag) ans *= -1;
if (ans > INT_MAX) return INT_MAX;
if (ans < INT_MIN) return INT_MIN;
return (int)ans;
}
};