class Solution {
public:
int strToInt(string str) {
int k=0;
int f=1;
while(k<str.size()&&str[k]==' ') k++;
if(k<str.size()&&str[k]=='-'){
f=-1;
k++;
}
if(str[k]=='+') k++;
long long res=0;
while(k<str.size()&&str[k]>='0'&&str[k]<='9'){
res=res*10+str[k]-'0';
if(res>INT_MAX) break;
k++;
}
res=res*f;
if(res>INT_MAX) return INT_MAX;
else if(res<INT_MIN) return INT_MIN;
return res;
}
};