反转整数

Posted on 2018-09-05 19:59  otonashi  阅读(151)  评论(0)    收藏  举报

给定一个 32 位有符号整数,将整数中的数字进行反转。

class Solution {
    public int reverse(int x) {
        int res=0;
        while(x!=0){
            int a=x%10;
            x/=10;
            if (res > Integer.MAX_VALUE/10 || (res == Integer.MAX_VALUE / 10 && a > 7)) return 0;
            if (res < Integer.MIN_VALUE/10 || (res == Integer.MIN_VALUE / 10 && a < -8)) return 0;
            res=res*10+a;
        }
        return res;
    }
}