美文网首页
9. Palindrome Number

9. Palindrome Number

作者: Icytail | 来源:发表于2017-10-25 21:06 被阅读0次

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的话感觉还是比较容易做的

相关文章

网友评论

      本文标题:9. Palindrome Number

      本文链接:https://www.haomeiwen.com/subject/zkffpxtx.html