LoginSignup
0
0

[1日1Leet Code] 09 Palindrome Number

Posted at

09 Palindrome Number
整数が与えられ、それがPalindromeかどうかを判定する問題。
Palindrome ... An integer is a palindrome when it reads the same forward and backward.

まず負の数の場合を除外。
与えられた整数を文字列とし、前から左右対称の位置の文字と一致するか見ていく。

if(String(x)[i] != String(x)[(String(x).length -1 ) - i]) の -1 をずっと忘れていて悩み苦しんだ。。。。。

palindromeNumber.js
/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(x) {
    let ans_bool = true;
    if(x < 0){
        ans_bool = false;
        return ans_bool;
    }
    for(let i = 0; i<(String(x).length + 1)/2; i++){
        if(String(x)[i] != String(x)[(String(x).length-1)  - i]){
            ans_bool = false;
        }
    }
    return ans_bool;
};
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0