C solution,6ms


  • 0
    B

    int myAtoi(char* str)
    {
    long val=0;
    int sign=1;
    while( *str && isspace(*str))
    str++;
    if (*str == '+')
    str++;
    else if (*str == '-')
    {
    sign = -1;
    str++;
    }
    while(*str && isdigit( str))
    {
    val=10
    val + *str++ - '0';

    if(val > INT_MAX)
            {
                if(sign == 1)
                return INT_MAX;
                else
                return INT_MIN;
            }   
    }
    
        return (val*sign);
    

    }


  • 0
    Z

    long val, good idea
    i use strcmp() to compare.


Log in to reply
 

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