My 5ms solution with C


  • -4
    2

    int _atoi(char* str)
    {
    int re_int = 0;
    if(str == NULL)
    return 0;
    while(isspace(*str))
    {
    str++;
    }
    // judge string is positive or negetive
    int flag = (*str) == '-' ? -1:1;
    if((*str) == '-'||(*str) == '+')
    {
    str++;
    }
    while(isdigit(*str))
    {
    if(INT_MAX / 10 >= re_int)
    re_int *= 10;
    else
    return flag == -1 ? INT_MIN:INT_MAX;
    if(INT_MAX - re_int >= (*str -'0'))
    re_int += ( *str - '0');
    else
    return flag == -1 ? INT_MIN:INT_MAX;
    str++;
    }
    return re_int * flag;
    }


Log in to reply
 

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