Palindrome Number
Problem statement
Given an integer x, return true if x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
- For example,
121is a palindrome while123is not.
Example 1:
Input: x = 121Output: trueExplanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: x = 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-231 <= x <= 231 - 1
My solution
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if (x < 0 || (x!== 0 && x % 10 === 0)) {
return false;
}
let nums = x;
let reverse = 0;
while (nums) {
// console.log("nums", nums)
reverse = (reverse * 10) + (nums % 10);
nums = Math.floor(nums / 10);
}
return x === reverse
};