首页 > 代码库 > LeetCode--Reverse Integer

LeetCode--Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

class Solution 
{
public:
    int reverse(int x) 
    {
        if(x>2147483647 || x <= -2147483648 || x == 0  )
            return 0;
        if(x < 0 )
            return (-reverse(-x));
        vector<int> temp;
        while(x > 0)
        {
            temp.push_back(x%10);
            x = x/10;
        }
        long long res = 0;
        for(int i=0; i<temp.size(); i++)
            res = res*10+temp[i];
        if(res>2147483647)
            return 0;
        return res;
    }
};


LeetCode--Reverse Integer