AcWing 87. 把字符串转换成整数
原题链接
简单
作者:
SayYong
,
2024-10-17 11:30:37
,
所有人可见
,
阅读 1
class Solution {
public:
int strToInt(string str) {
int k = 0;
while (k < str.size() && str[k] == ' ') k++;
int flag = false;
long long number = 0;
if (str[k] == '+') k++;
else if (str[k] == '-') k++, flag = true;
while (k < str.size() && str[k] >= '0' && str[k] <= '9') {
number = number * 10 + str[k] - '0';
if (number > 1e11) break;
k++;
}
if (flag) number *= (-1);
if (number > INT_MAX) number = INT_MAX;
else if (number < INT_MIN) number = INT_MIN;
return number;
}
};