https://leetcode.com/problems/palindrome-number/
var isPalindrome = function(x) {
if (x < 0) return false;
let arr = x.toString().split('');
let halfArr = arr.slice(0, Math.floor(arr.length / 2));
return halfArr.every((item, index) => item === arr[arr.length - 1 - index]);
};
we can reverse number then check with given number
var isPalindrome = function(x) {
if (x < 0) return false;
let result = 0;
for (let i = x; i >= 1; i = Math.floor(i / 10)) {
result = result * 10 + i % 10;
}
return x === result;
};
using some api with Array
var isPalindrome = function (x) {
x = x.toString().split('');
while (x.length > 1) {
if (x.pop() !== x.shift()) {
return false;
}
}
return true;
};
网友评论