My C solution, 6ms


  • 1
    P
    int reverse(int x) {
    long long num = x;
    int n_flag = 1;
    long long result = 0;
    if (x < 0) {
    	num = -x;
    	n_flag = -1;
    }
    while( num > 0 ) {
        result = result * 10 + num % 10;
        num /= 10;
    }
    if ( result > 0x7FFFFFFF || result < (-0x7FFFFFFF - 1) ) {
        return 0;
    }
    return n_flag * result;
    }

  • 0
    Y

    because result>0,so
    if ( result > 0x7FFFFFFF || result < (-0x7FFFFFFF - 1) ) can write as:
    if ( result > 0x7FFFFFFF)


Log in to reply
 

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