Someone help me~ why my c code doesn't work?


  • 0
    B
    int myAtoi(char* str) {
        int x=0;
        int sign=1;
    
        //skip blanks
        while(*str==' ') str++;
        if (strlen(str)==0) return 0;
    
        //check sign
        if(*str=='+' || *str=='-')
            sign = *str++ == '+' ? 1 : -1;
    
    
        while(*str <='9' && *str >='0'){
            if(LONG_MAX/10 < x || (LONG_MAX/10 == x && LONG_MAX %10 < *str-'0'))
                    return sign == 1 ? LONG_MAX : LONG_MIN;
            x = x*10 + *str++ -'0';
    
        }
    
        return sign*x;
    }
    

    This is bug report ~

    1. Input
      "2147483648"
    2. Output
      -2147483648
    3. Expected
      2147483647

    Is is work fine in Code::Blocks 13


Log in to reply
 

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