Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 判断是否为回文数,比较简单,可以用两种方式做,一种是用java中的stringbuffer的一个反转函数,一种直接判断数字。后一种效率比较高。前者代码量少点。 1: public class Solution { public boolean isPalindrome(int x) { StringBuffer sb = new StringBuffer(); sb.append(x); StringBuffer sb1 = new StringBuffer(sb); sb.reverse(); return sb.toString().equals(sb1.toString()); } } 2: public class Solution { public boolean isPalindrome(int x) { int a = x; int b = 0;
while (a>=b)
{
if(x%10 == 0 && x != 0){
return false;
}
if (a == b) return true;
b = 10 * b + a % 10;
if (a == b) return true;
a = a / 10;
}
return false;
}
}