class Solution {
public:
int myAtoi(string s) {
if(s.empty()) return 0;
// 去掉空格
int k = 0;
while(s[k] == ' ') k++;
bool flag = false;
if(s[k] == '-') k++, flag = true;
else if(s[k] == '+') k++;
long long res = 0;
while(s[k] >= '0' && s[k] <= '9'&& k < s.size()){
res = res * 10 + (s[k] - '0');
if(flag && -res < INT_MIN) return INT_MIN;
else if(!flag &&res > INT_MAX) return INT_MAX;
k++;
}
return flag ? - res :res;
}
};