Problem
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, 121 is palindrome while 123 is not.
Example
Input: x = 121Output: true
Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: x = 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome.
Input: x = -101Output: false
Solution
使用双指针的方式,i从前往后遍历,j从后往前遍历
/*** @param {number} x* @return {boolean}*/var isPalindrome = function (x) {if (x < 0) {return false;}const nums = [];while(x > 0) {nums.push(x % 10);x = parseInt(x / 10);}for (let i = 0, j = nums.length - 1; j > i; j--, i++) {if (nums[j] !== nums[i]) {return false;}}return true;};
只需要判断反转后的数与原数相等,则返回true,否则返回false ```javascript /**
- @param {number} x
@return {boolean} */ var isPalindrome = function (x) { if (x < 0) {
return false;
} const oriX = x;
var revertX = 0;
while(x > 0) {
revertX = revertX * 10 + x % 10; x = parseInt(x / 10);}
return revertX === oriX; }; ```
