Strange expected output 2147483647


  • 0
    G

    My code is like:

    long long myAtoi(char* str) {
        if(str == NULL || strlen(str) == 0) {
            return 0;
        }
        
        int i=0;
        int flag;
        long long ret=0;
        while(str[i] == ' ') i++;
        if(str[i] >= '0' && str[i] <= '9') {
            flag = 1;
        } else if(str[i] == '-') {
            flag = -1;
            i++;
        } else if(str[i] == '+') {
            flag = 1;
            i++;
        } else {
            return 0;
        }
        
        while(i < strlen(str)) {
            if(str[i] >= '0' && str[i] <= '9') {
                ret = ret*10;
                if(ret > INT_MAX)
                    return 0;
                    
                ret += str[i] - '0';
            } else {
                break;
            }
            i++;
        }
        
        ret *= flag;  
        return (ret > INT_MAX || ret < INT_MIN) ? 0 : ret;
    }
    

    But it gave me a strange test case.

    Input:	        "2147483648"
    Output:	        0
    Expected:	2147483647
    

    Is there something wrong with the judging system?


  • 1
    W

    2147483647 is INT_MAX, so any result more than this number , the result is always 2147483647 .
    the judging system is right in this case.


Log in to reply
 

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