[LeetCode-7] Reverse Integer(反转整数)

Reverse digits of an integer.

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

click to show spoilers.

提交代码的时候,发现需要对边界进行处理,如果出现溢出的情况需要,做return 0;操作。见代码:

int reverse(int x) {
    long long reverseNum = 0;
    long long temp = 0;

    const int max = 0x7fffffff;  //int最大值 
    const int min = 0x80000000;  //int最小值 

    int flag;

    while(x) {
        temp = x % 10;
        reverseNum = 10*reverseNum + temp;
        if(reverseNum > max || reverseNum < min) {   
            return 0;
        }
        x = x/10;       
    }

    return (int)reverseNum;
}

你可能感兴趣的:(Integer,reverse)