9. Palindrome Number

Leetcode

https://leetcode.com/problems/palindrome-number/

題目

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.

解答

  • 方法一

var isPalindrome = function(x) {
    const str = x.toString();
    const len = str.length;
    for(let i=0; i<len/2; i++) {
        const right = len - i - 1;
        if(str[i] !== str[right]) return false
    }
    return true;
};

Runtime: 176 ms, faster than 84.13% of JavaScript online submissions for Palindrome Number.

Memory Usage: 47.5 MB, less than 95.68% of JavaScript online submissions for Palindrome Number.

測資

let x = 121; // true
x = -101; // false

console.log(isPalindrome(x));

Last updated

Was this helpful?