Simplest Solution ever O(n) time O(1) space


#1
int Solution::atoi(const string s) 
{
    int i = 0;
    while(s[i] == ' ')
    {
        i++;
    }
    
    long long l = 0, f = 1;
    if(s[i] == '-')
    {
        f = -1;
        i++;
    }
    else if(s[i] == '+') i++;
    
    while(s[i] >= 48 && s[i] <= 57)
    {
        l = l * 10 + (s[i] - 48);
        i++;

        if(l > INT_MAX)
        {
            if(f == -1) return INT_MIN;
            return INT_MAX;
        }
    }
    
    if(f == -1) return (int)(f * l);
    return (int)l;
}