9判断整数是否为回文数

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

先翻转再判断,好像有点复杂

class Solution {
    public boolean isPalindrome(int x) {
        if(x==0)
            return true;
        if(x>0){            
            int rex = reverse(x);
            return x==rex;
            }
        return false;
    }

    public int reverse(int x){
        int res=0;
        while(x!=0){
            int remainder = x%10;
            x=x/10;
            if(res>Integer.MAX_VALUE/10||res<Integer.MIN_VALUE/10){
                return 0;
            }
            res = res*10 + remainder;
        }
        return res;
    }
}
posted @ 2020-08-28 13:42  _zjk  阅读(102)  评论(0)    收藏  举报