1. x == reverse(x)?
2. 算术运算
public class Solution {
// 1. could negative number be Palindrome?
// 2. reverse Integer!
public boolean isPalindrome(int x) {
if (x < 0) return false;
return x == reverseInt(x);
}
public int reverseInt(int x) {
int res = 0;
while(x != 0){
res *= 10;
res += x%10;
x = x/10;
}
return res;
}
}
public class Solution {
//法二
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
//find out how many digits it has
int div = 1;
while (x/div >= 10) {
div *= 10;
}
while (x != 0) {
int left = x / div;
int right = x % 10;
if (right != left) {
return false;
}
x = x % div / 10;
div /= 100;
}
return true;
}
}