回文判断

回文数字

http://www.nowcoder.com/questionTerminal/35b8166c135448c5a5ba2cff8d430c32

题目链接:https://www.nowcoder.com/practice/35b8166c135448c5a5ba2cff8d430c32?tpId=46&rp=1&ru=%2Fta%2Fclassic-code&qru=%2Fta%2Fclassic-code%2Fquestion-ranking

public class Solution {
    /**
     *
     * @param x int整型
     * @return int整型
     */
    public boolean isPalindrome (int x) {
        int num = 0;
        int xx = x;
        if(x < 0){
            return false;
        }
        while(x != 0){
            num = num*10+(x%10);
            x = x / 10;
        }
        if(num > Integer.MAX_VALUE && num < Integer.MIN_VALUE){
            return false;
        }
        return xx == (int) num;
    }
}

首先,自己能想到使用翻转数字来做是一个很好的思路,但是自己没有考虑到,负数不能作回文数,所以导致浪费了很多的时间,还有一个很简单的错误,命名可以直接让xx = x,这样就能保留x的值,最后再与num比较,这样就可以省去考虑个位数的麻烦了(因为x/10,如果x为个位数则结果为0)

新方法:return xx == (int) num;

省去了if判断的麻烦

全部评论

相关推荐

10-31 20:07
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
12-18 11:21
优秀的大熊猫在okr...:叫你朋友入职保安,你再去送外卖,一个从商,一个从政,你们两联手无敌了,睁开你的眼睛看看,现在是谁说了算(校长在背后瑟瑟发抖)
选实习,你更看重哪方面?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务