class Solution {
public:
int atoi(const char *str) {
if(str==NULL){ //1
return 0;
}
int i=0;
long long res=0;
while(isspace(str[i])){//2
i++;
}
bool neg=false;
if(str[i]=='-'){ //3
neg=true;
i++;
}else if(str[i]=='+'){
i++;
}
while(str[i]!='\0'){ //4
if(isdigit(str[i])){
res=res*10+str[i]-'0';
if(res>INT_MAX){
return neg?INT_MIN:INT_MAX;
}
i++;
}else{
return neg?-res:res;
}
}
return neg?-res:res;
}
};