Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

没什么好说的,简单题,注意超范围的话返回0;

class Solution {
public:
    int reverse(int x) {
        long flag = 1,res=0;
        if (x < 0) {
            flag = -1;
            x = abs(x);
        }
        while (x > 0) {
            res = res * 10 + x % 10;
            x /= 10;
        }
        res *= flag;
        return (res<INT32_MAX&&res>INT32_MIN) ? res : 0;
    }
};