回文判断
回文数字
http://www.nowcoder.com/questionTerminal/35b8166c135448c5a5ba2cff8d430c32
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判断的麻烦