森田賢二のleetCodeの使い方、解説、解答を毎日更新しているキャラクター

[leetcode] 問題解答と解説。125. Valid Palindrome [JavaScript]

[leetcode]問題解答と解説。125. Valid Palindrome [JavaScript]

皆さんleetCode好きですよね。私も好きです。
なかなかやる機会がないのですよね。
leetCodeの使い方や解説ではなく実際に問題を解決するために何をしたか
をメモ書きして行きます

私はある決意をして今挑んでいます。
モチベーションを保つためだけにこれをやっています。

では今日のleetCode。
【125. Valid Palindrome】の解決策と方法。自分がまず挑戦して
やったことをさらけ出します。

※下にある自分のコードは自分で設けた制限時間内に書けたところまです。

これは
「私は年末までこれを続けたらどんなleetCoderになるか」のシリーズ。
実験的ブログ更新です。

森田賢二のleetCode
森田賢二のleetCode

[leetcode] 125. Valid Palindrome ルール

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

ざっくり和訳

フレーズは、すべての大文字を小文字に変換し、英数字以外のすべての文字を削除した後、同じ前方と後方を読み取る場合、回文です。 英数字には文字と数字が含まれます。

文字列sを指定すると、回文の場合はtrueを返し、それ以外の場合はfalseを返します。

exampleは実際の問題ページを見てください

Example 1:

Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.

Example 3:

Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.

[leetcode]125. Valid Palindrome 問題ページ

125. Valid Palindrome

[leetcode]125. Valid Palindrome

my code is not good(clear)

ちょっと微調整している..

function isPalindrome(s: string): boolean {
    if(s === " " || s === ".") return true
    const s1 = s.toLowerCase()
    const search = s1.replace(/[^a-zA-Z]+/g, "")
    if(search.length <= 2) return false
    const flo = search.length / 2
    const iss = flo % 2 === 0
    if(iss){
        return false
    }
    const mid = Math.floor(flo) + 1
    let first = 0;
    let last = search.length -1;

    while(first  < last){
        if(search.substr(first, 1) !== search.substr(last, 1)){
            return false
        }
        first++
        last--
    }
    return true
};

[leetcode]125. Valid Palindrome。discussの中の一つの解答例

数ある中からJavaScriptのもので、理解しやすい解説をピックアップしました。
discussから見ればさらにもっと違う方法で真似したくなるものがあるかもしれないですね

JavaScriptでfilterされている、discussはこちらから

これが違う実装

leetCode 125. Valid Palindrome

1

/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function(input) {
    var start = 0
    var end = input.length - 1
    while (start < end) {
        var s = input.charCodeAt(start)
        var e = input.charCodeAt(end) // codeを使う

        if (!isLetter(s)) {
            start++
            continue
        }
        if (!isLetter(e)) {
            end--
            continue
        }

        if (toLowerCase(s) !== toLowerCase(e)) {
            return false 
        } 
        start++
        end--
  }
  return true
};

var isLetter = function(code) {
    if (((code >= 48) && (code <= 57))  // numbers。ここら辺使うの手練れ感すごいな...
    || ((code >= 65) && (code <= 90))  // uppercase
    || ((code >= 97) && (code <= 122))) {  // lowercase
        return true
    }
    else {
        return false
    }
}

var toLowerCase = function(code) {
    if (code >= 65 && code <= 90) {
        return code + 32    
    }
    else {
        return code
    }
}

こっちの方がやりたかったことに近い

https://leetcode.com/problems/valid-palindrome/discuss/554098/Simple-JavaScript-soluton

const isPalindrome = s => {
  s = s.toLowerCase().replace(/[^a-z0-9]/gi,'');
  for (let i = 0, j = s.length - 1; i <= j; i++, j--) { // nice!!!
    if (s.charAt(i) !== s.charAt(j)) return false;
  }
  return true;
}

まあそうなんだけれど回答

var isPalindrome = function(s) {
  var cleaned = s.replace(/\W/g, "");
  var reversedAndCleaned = cleaned
    .split("")
    .reverse()
    .join("");

  return cleaned.toLowerCase() == reversedAndCleaned.toLowerCase();
};

[leetcode]125. Valid Palindromeをやってみて感想

  • /[^a-z0-9]/gi

カテゴリー leetCode