Palindrome Number
Example
Input:
121
Output:
trueInput:
-121
Output:
false
Explanation:
From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.Note
Code
Last updated
Input:
121
Output:
trueInput:
-121
Output:
false
Explanation:
From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.Last updated
Input:
10
Output:
false
Explanation:
Reads 01 from right to left. Therefore it is not a palindrome.public boolean isPalindrome(int x) {
if( x < 0 || (x != 0 && x % 10 == 0)) return false;
int num = x;
int r = 0;
while (x > r) {
r = r * 10 + x % 10;
x /= 10;
}
return (x == r || x == r / 10);
}