0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Leet Code] 09 Palindrome Number

Last updated at Posted at 2024-05-04

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?