Palindrome Number
Given an integer x, determine whether it's a numeric palindrome — that is, whether it reads identically from left to right and right to left.
Input Format
Implement isPalindrome(x). Inputs below show JavaScript function arguments; there is no stdin parsing.
Output Format
Return the result described in the statement.
Sample Input and Output
Example 1 · Run Sample
Input
x = 121 Output
true Example 2
Input
x = -121 Output
false Example 3
Input
x = 10 Output
false Constraints
- -2^31 <= x <= 2^31 - 1
Test Cases (3)
Case 1
Args: [121]
Expected: true
Case 2
Args: [-121]
Expected: false
Case 3
Args: [10]
Expected: false
Reverse Half
function isPalindrome(x) {
if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
let rev = 0;
while (x > rev) { rev = rev * 10 + x % 10; x = Math.floor(x / 10); }
return x === rev || x === Math.floor(rev / 10);
}Time: O(log n) | Space: O(1)
Saved in this browser only. Private to you.