1. negative is not palindrome number
2. use double to avoid the overflow.
public boolean isPalindrome(int x) {
if (x < 0) return false;
if (x == 0) return true;
double rev = 0;
int y = x; //don't forget to save the original number before you do some operation.
while (x != 0) {
rev = rev * 10 + x%10;
x /= 10;
}
if (rev > Integer.MAX_VALUE) {
return false;
}
return (int)rev == y ;
}
No comments:
Post a Comment