首页 > 代码库 > LeetCode Reverse Integer

LeetCode Reverse Integer

Reverse digits of an integer.

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

click to show spoilers.

Show Tags










题意:将数字反转

思路:注意要判断溢出就是了


class Solution {
public:
    int reverse(int x) {
        long long ans = 0;
        while (x) {
            ans = ans * 10 + (x % 10);
            x /= 10;
        }
        
        if (ans > INT_MAX)
            ans = 0;
        if (ans < INT_MIN)
            ans = 0;
        return ans;
    }
};






LeetCode Reverse Integer