7. Reverse Integer

    xiaoxiao2021-04-19  162

    Reverse digits of an integer.

    Example1: x = 123, return 321 Example2: x = -123, return -321

    Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

    int reverse(int x) { int maxint = 0x7FFFFFFF; int minint = 0x80000000; int flag = 0; long long result = 0; if( x == minint ) return 0; if( x < 0 ){ flag = 1; x = -x; } while( x > 0 ){ result *= 10; result += x % 10; if( result > maxint ) return 0; x /= 10; } if( flag ) return -result; return result; }
    转载请注明原文地址: https://ju.6miu.com/read-676208.html

    最新回复(0)