首页 > 其他 > 详细

9判断整数是否为回文数

时间:2020-08-28 14:21:26      阅读:90      评论:0      收藏:0      [点我收藏+]

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

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

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;
    }
}

9判断整数是否为回文数

原文:https://www.cnblogs.com/jackiez/p/13577009.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!