Cpp solution easy to understand


#1
int Solution::atoi(const string A) {
string s=A;
int flag=1;

while(s[0]==' ')
    s.erase(s.begin());
    

if(s[0]=='-') {
    flag= -1;
    s.erase(s.begin());
    }
    
if(s[0]=='+') 
    s.erase(s.begin());
    

long k=0,i=0;


while(s[i] >= 48 && s[i] <= 57)
{
    k = k * 10 + (s[i] - 48);
    i++;

    if(k> INT_MAX)
    {
        if(flag == -1) return INT_MIN;
        return INT_MAX;
    }
}


return flag*k;

}