C solution (6ms)


  • 1
    A
    int myAtoi(char* str) {
        if(*str == NULL)
        {
            return 0;
        }
        int res = 0;
        int sign = 1;
        int i =0;
        
        while(isspace(*str)) str++;
        
        if(str[0] == '-')
        {
            sign = -1;
            i++;
        }
        else if(str[0] == '+')
        {
            sign = +1;
            i++;
        }
        else
        {
            sign = +1;
        }
        
        while (str[i] >= '0' && str[i] <= '9') {
            if (res >  INT_MAX / 10 || (res == INT_MAX / 10 && str[i] - '0' > 7)) {
                if (sign == 1) return INT_MAX;
                else return INT_MIN;
            }
            res  = 10 * res + (str[i++] - '0');
        }
        return sign * res;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.