题目:判断一个数字是否是回文数。
直接上代码:
class Solution {
public boolean isPalindrome(int x) {
if(x<0)
return false;
int res=0;
int temp=x;
while(x!=0){
res=res * 10+x % 10;
x /= 10;
}
return res==temp;
}
}
看到另一种解法,别人只比较了数字的一半;
class Solution {
public boolean isPalindrome(int x) {
if(x<0||(x!=0&&x%10==0))//小于零以及整十的倍数直接返回false
return false;
int res=0;
while(x>res){
res=res * 10+x % 10;
x /= 10;
}
return res==x||x==res/10;//第一个判断是当x为偶数时,第二个判断是当x为奇数时
}
}