就喜欢做水题😄
C++ 代码
class Solution {
public:
int strToInt(string str) {
long long a = 0;
int s = 0;
while (str[s] == ' ')s++;
bool is_ = false;
if (str[s] == '-')is_ = true, s++;
else if (str[s] == '+')s++;
while (str[s] <= '9' && str[s] >= '0') {
if (is_)
a = a * 10 - (str[s] - '0');
else a = a * 10 + str[s] - '0';
if (a > INT_MAX)return INT_MAX;
if (a < INT_MIN)return INT_MIN;
s++;
}
return a;
}
};