首页 > 代码库 > Leetcode-7 Reverse Integer

Leetcode-7 Reverse Integer

#7. Reverse Integer          

Reverse digits of an integer.

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

题解:暴力做,注意边界:

class Solution {
public:
    int reverse(int x) {
        long long num=abs(x);
        long long ret=0;
        int tmp=0;
        while(num>9)
        {
            tmp=num%10;
            ret=ret*10+tmp;
            num/=10;
        }
        ret=ret*10+num;
        
        if(ret>INT_MAX||x==INT_MIN)
        return 0;
        else if(x<0)
        return -ret;
        else
        return ret;
    }
};

  

Leetcode-7 Reverse Integer