美文网首页
9. Palindrome Number

9. Palindrome Number

作者: 夏臻Rock | 来源:发表于2017-12-21 16:20 被阅读0次

题目:判断一个数是不是回文数,注意负数不是回文数

思路:求出该数的倒置数,判断是否相等即可。

代码:

class Solution {
    public boolean isPalindrome(int x) {
       if(x<0){
            return false; //负数不是回文数字
        }
        int ax =0;
        int b = x;
        while(b>0){
            ax = ax*10+b%10;
            b/=10;
        }
        if(ax == x){
            return true;
        }else return false;
    }
}

相关文章

网友评论

      本文标题:9. Palindrome Number

      本文链接:https://www.haomeiwen.com/subject/touawxtx.html