AcWing 87. 把字符串转换成整数
原题链接
简单
作者:
深街酒徒
,
2024-12-02 16:03:00
,
所有人可见
,
阅读 1
class Solution {
public:
int strToInt(string str) {
int i = 0;
while(i < str.size() && str[i] == ' ') i++;
int flag = 1;
if(str[i] == '-') flag = -1, i++;
else if (str[i] == '+') i++;
long long res = 0;
while(i < str.size() && str[i] >= '0' && str[i] <= '9')
{
res = res * 10 + str[i] - '0';
i++;
cout << "res = " << res << endl;
if(res > 1e10) break;
}
res *= flag;
// cout << "res = " << res << endl;
if(res > INT_MAX) return INT_MAX;
if(res < INT_MIN) return INT_MIN;
return res;
}
};