Description:
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
My code:
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
var str = x.toString();
if(x >= 0) {
var reverseNum = parseInt(str.split("").reverse().join(""));
if(reverseNum == x) {
return true;
} else {
return false;
}
} else {
return false;
}
};
Note: 负数全部不算回文数;刚开始不知道without extra space是什么意思,看了github的issue才了解到应该是指复杂度应为1,用js的话感觉还是比较容易做的
网友评论