9. Palindrome Number
Given an integer x, return true if x is a palindrome, and false otherwise.
palindrome: An integer is a palindrome when it reads the same forward and backward.
Input: x = 121
Output: true
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: x = 10
Output: false
Solution
function isPalindrome(x: number): boolean {
if (x < 0) return false;
if (x < 10) return true;
const stringNum = x.toString();
let left = 0;
let right = stringNum.length - 1;
while (right > left) {
if (stringNum[left] !== stringNum[right]) return false;
left++;
right--;
}
return true;
}