From b02e3474463f57b880aea33b22aa40595f25ee80 Mon Sep 17 00:00:00 2001 From: shintaro202020 Date: Tue, 8 Apr 2025 08:52:23 +0900 Subject: [PATCH 01/17] feat : #10 add the empty file of "20. Valid Parentheses" --- stack/valid-parentheses/answer.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 stack/valid-parentheses/answer.md diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md new file mode 100644 index 0000000..e69de29 From 2a2aed0d2426b2b0297e3d257e191c05fc1f9193 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Tue, 8 Apr 2025 11:28:02 +0900 Subject: [PATCH 02/17] feat : #10 test commit with new gpg signkey --- stack/valid-parentheses/answer.md | 1 + 1 file changed, 1 insertion(+) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index e69de29..7aba4a3 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -0,0 +1 @@ +# 20. Valid Parentheses From 8a197ff72df86901f11632115d74e52fa162ee3c Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Wed, 9 Apr 2025 09:41:30 +0900 Subject: [PATCH 03/17] feat : #10 finish the STEP1/STEP2/STEP3 of "20. Valid Parentheses" --- stack/valid-parentheses/answer.md | 180 ++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 7aba4a3..430c5a3 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -1 +1,181 @@ # 20. Valid Parentheses + +## STEP 1 + +### 手作業でやってみる。 + +* `{`, `[`, `(`, `}`, `]`, `)` と書いた紙が一列に並んでいる +* 一番最初の紙から作業をしていく。 +* 紙が`{`, `[`, `(` だったら、机の上に縦に積んでいく。 +* 紙が`}`, `]`, `)` だったら、机の上の一番上から紙を取り、同じ種類の紙かを確認する。 +* 作業が完了して、机の上に何も残っていない場合には、終了する。 +* 机の上に何か残っている場合には、異常を報告する。 + +``` +var isValid = function(characters) { + const container = [] + const open_bracket_chars = ["(", "{", "["] + for (const character of characters) { + if (open_bracket_chars.includes(character)) { + container.push(character) + } + if (character === ")") { + const last_character = container.pop() + if (last_character !== "(") { + return false + } + } + if (character === "]") { + const last_character = container.pop() + if (last_character !== "[") { + return false + } + } + if (character === "}") { + const last_character = container.pop() + if (last_character !== "{") { + return false + } + } + } + if (container.length === 0) { + return true + } + return false +}; +``` +## STEP 2 + +### やったこと +* 関数を分けた. + +``` +function doesMatchBracket(candidate, close_bracket) { + if (candidate === "[" && close_bracket === "]") { + return true + } + if (candidate === "{" && close_bracket === "}") { + return true + } + if (candidate === "(" && close_bracket === ")") { + return true + } + + return false +} +var isValid = function(characters) { + const container = [] + const open_bracket_chars = ["(", "{", "["] + + for (const character of characters) { + // 開き括弧の場合 + if (open_bracket_chars.includes(character)) { + container.push(character) + continue + } + + // 閉じ括弧の場合 + const open_bracket_candidate = container.pop() + const close_bracket = character + if (!doesMatchBracket(open_bracket_candidate, close_bracket)) { + return false + } + } + if (container.length === 0) { + return true + } + + return false +}; +``` + +## STEP 3 + +``` +/** + * @param {string} s + * @return {boolean} + */ +function doesMatchBracket(candidate, close_bracket_character) { + if (candidate === "(" && close_bracket_character === ")") { + return true + } + if (candidate === "[" && close_bracket_character === "]") { + return true + } + if (candidate === "{" && close_bracket_character === "}") { + return true + } + return false +} +var isValid = function(characters) { + const container =[] + const open_bracket_characters = ["(", "[", "{"] + + for (const character of characters) { + if (open_bracket_characters.includes(character)) { + container.push(character) + continue + } + const open_bracket_candidate = container.pop() + const close_bracket_character = character + if (!doesMatchBracket(open_bracket_candidate, close_bracket_character)) { + return false + } + } + + if (container.length === 0) { + return true + } + return false +}; +``` + +## 他の人のコードを読んで + +* BumbuShoji のPR https://github.com/BumbuShoji/Leetcode/pull/7 + * 開き括弧と閉じ括弧の対応関係を表すMapを用意しておく方法がある (`*1`) + +* hayashi-ay のコメント https://github.com/BumbuShoji/Leetcode/pull/7#discussion_r1810574515 + 最後のif文は、以下のコードでも良い. + * 変更前 + + ``` + if (container.length === 0) { + return true + } + return false + ``` + + * 変更後 + + ``` + return container.length === 0 + ``` + +## その他の解法 + +* `*1` + +``` +var isValid = function(characters) { + const open_to_close = new Map() + open_to_close.set("(", ")") + open_to_close.set("{", "}") + open_to_close.set("[", "]") + + const container =[] + for (const character of characters) { + if (open_to_close.has(character)) { + container.push(character) + continue + } + const converted_last_character = open_to_close.get(container.pop()) + const close_bracket_character = character + if (converted_last_character !== close_bracket_character) { + return false + } + } + return container.length === 0 +}; +``` From 9ee32258c6fd489d94bea45e4d54b3a047847790 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Wed, 9 Apr 2025 10:13:38 +0900 Subject: [PATCH 04/17] feat : #10 add the impression of "20. Valid Parentheses" --- stack/valid-parentheses/answer.md | 94 ++++++++++++++++++++++++++----- 1 file changed, 79 insertions(+), 15 deletions(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 430c5a3..54b9412 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -130,32 +130,44 @@ var isValid = function(characters) { return false }; ``` +## 感想 -## 他の人のコードを読んで +### 他の人のコードを読んで * BumbuShoji のPR https://github.com/BumbuShoji/Leetcode/pull/7 * 開き括弧と閉じ括弧の対応関係を表すMapを用意しておく方法がある (`*1`) - -* hayashi-ay のコメント https://github.com/BumbuShoji/Leetcode/pull/7#discussion_r1810574515 - 最後のif文は、以下のコードでも良い. - * 変更前 - + + * はじめに、閉じ括弧があるケースを想定できていなかった。 + * 配列が要素数0の時に、pop()で、undefinedを返すため、たまたま上手く行った。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop#return_value + * 変更前 ``` - if (container.length === 0) { - return true - } - return false + const open_bracket_candidate = container.pop() ``` - - * 変更後 - + * 変更後 ``` - return container.length === 0 + const open_bracket_candidate = container.pop() || "" + const open_bracket_candidate = container.length > 0 ? container.pop() : "" ``` + * 最後のif文は、`return container.length === 0`、`return !arr.length` でも良い + * 変更前 + + ``` + if (container.length === 0) { + return true + } + return false + ``` + + * 変更後 + + ``` + return container.length === 0 + ``` + ## その他の解法 -* `*1` +* `*1` 括弧の対応関係を表すMapを使う方法 ``` var isValid = function(characters) { @@ -179,3 +191,55 @@ var isValid = function(characters) { return container.length === 0 }; ``` + +* SanakoMeine のPR https://github.com/SanakoMeine/leetcode/pull/7 + * 以下のコードが簡潔で読みやすい. + + +* `*2` 番兵をおく方法 + + * 番兵をおく方法がる. (`*2`) + * 以下の方法でさらに簡潔にかける. + ``` + const bracket_pairs = new Map([ + ["{", "}"], + ["(", ")"], + ["[", "]"], + ["*", ""], + ]) + const container = ["*"] + ``` + +``` + +function doesMatchBracket(candidate, close_bracket_character) { + if (candidate === "(" && close_bracket_character === ")") { + return true + } + if (candidate === "[" && close_bracket_character === "]") { + return true + } + if (candidate === "{" && close_bracket_character === "}") { + return true + } + return false +} +var isValid = function(characters) { + const container =[] + const open_bracket_characters = ["(", "[", "{"] + container.push("SENTINEL") + + for (const character of characters) { + if (open_bracket_characters.includes(character)) { + container.push(character) + continue + } + const open_bracket_candidate = container.pop() + const close_bracket_character = character + if (!doesMatchBracket(open_bracket_candidate, close_bracket_character)) { + return false + } + } + return container.length === 1 && container[container.length-1] === "SENTINEL" +}; +``` From d9b24eae09315bf821659065eb8d8fb99926aec3 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Wed, 9 Apr 2025 11:32:30 +0900 Subject: [PATCH 05/17] feat : #10 add the impression of "20. Valid Parentheses" --- stack/valid-parentheses/answer.md | 38 ++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 54b9412..2b343c5 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -132,6 +132,11 @@ var isValid = function(characters) { ``` ## 感想 +### 一番ためになったこと + +* 不正な入力に対して、エラーハンドリングをどうするかという視点がなかった。 + https://github.com/lilnoahhh/leetcode/pull/7#discussion_r1948110757 + ### 他の人のコードを読んで * BumbuShoji のPR https://github.com/BumbuShoji/Leetcode/pull/7 @@ -165,6 +170,38 @@ var isValid = function(characters) { return container.length === 0 ``` +* Odaのコメント https://discord.com/channels/1084280443945353267/1225849404037009609/1231646037077131315 + * エラーハンドリングで、括弧以外が来るという一般化を考慮できていなかった。 + > open_to_close でデータは持ちたいです。文字列にカッコ以外が来たときに落ちないで欲しいからです。 + + * 括弧以外の文字が入ってきた際のあるべきは、エラーログを吐き、exit 1で異常終了することと考えた。 https://discord.com/channels/1084280443945353267/1225849404037009609/1231648833914802267 + * 例外を投げる方法がある。 https://discord.com/channels/1084280443945353267/1227464441235509308/1228599642200211496 + * 異常終了、続行する(例外を投げる、特殊な値を返す)の選択肢を意識して、選ぶようにする。 + * 異常終了 + * エラーに気づきやすい。 https://github.com/mura0086/arai60/pull/11#discussion_r1986104852 + + 参考 : https://discord.com/channels/1084280443945353267/1226508154833993788/1227171332131786774 + + * 異常な入力への対応を頭に入れてコードを書く。 + * 質問されたら、結果と選択肢と理由を回答できる状態にする。 https://discord.com/channels/1084280443945353267/1316770883729100810/1335077966954369095 + +* lilnoahhhのPR https://github.com/lilnoahhh/leetcode/pull/7 + * Stringで判定する方法がある。 + * 変更前 + ``` + const open_bracket_chars = ["(", "{", "["] + if (open_bracket_chars.includes(character)) { + // + } + ``` + ``` + const open_brackets = "{([" + if (open_brackets.includes(character)) { + // + } + ``` + * ボトルネックではないところは、最適化しない。 + ## その他の解法 * `*1` 括弧の対応関係を表すMapを使う方法 @@ -195,7 +232,6 @@ var isValid = function(characters) { * SanakoMeine のPR https://github.com/SanakoMeine/leetcode/pull/7 * 以下のコードが簡潔で読みやすい. - * `*2` 番兵をおく方法 * 番兵をおく方法がる. (`*2`) From b24bea8fe65428bca0f048991746f7e4dfb7201a Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Wed, 9 Apr 2025 12:55:06 +0900 Subject: [PATCH 06/17] feat : #10 add unnecessary comment in "20. Valid Parentheses" --- stack/valid-parentheses/answer.md | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 2b343c5..6d20768 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -140,18 +140,19 @@ var isValid = function(characters) { ### 他の人のコードを読んで * BumbuShoji のPR https://github.com/BumbuShoji/Leetcode/pull/7 - * 開き括弧と閉じ括弧の対応関係を表すMapを用意しておく方法がある (`*1`) + * 開き括弧と閉じ括弧の対応関係を表すMapを用意することも可能。 (`*1`で解法を追加) * はじめに、閉じ括弧があるケースを想定できていなかった。 - * 配列が要素数0の時に、pop()で、undefinedを返すため、たまたま上手く行った。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop#return_value - * 変更前 - ``` - const open_bracket_candidate = container.pop() - ``` - * 変更後 - ``` - const open_bracket_candidate = container.pop() || "" - const open_bracket_candidate = container.length > 0 ? container.pop() : "" + * 配列が要素数0の時に、pop()で、undefinedを返すため、たまたま上手く行った。 + 参考 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop#return_value + * 変更前 + ``` + const open_bracket_candidate = container.pop() + ``` + * 変更後 + ``` + const open_bracket_candidate = container.pop() || "" + const open_bracket_candidate = container.length > 0 ? container.pop() : "" ``` * 最後のif文は、`return container.length === 0`、`return !arr.length` でも良い @@ -171,12 +172,11 @@ var isValid = function(characters) { ``` * Odaのコメント https://discord.com/channels/1084280443945353267/1225849404037009609/1231646037077131315 - * エラーハンドリングで、括弧以外が来るという一般化を考慮できていなかった。 + * エラーハンドリングの観点を持っていなかった。 > open_to_close でデータは持ちたいです。文字列にカッコ以外が来たときに落ちないで欲しいからです。 * 括弧以外の文字が入ってきた際のあるべきは、エラーログを吐き、exit 1で異常終了することと考えた。 https://discord.com/channels/1084280443945353267/1225849404037009609/1231648833914802267 - * 例外を投げる方法がある。 https://discord.com/channels/1084280443945353267/1227464441235509308/1228599642200211496 - * 異常終了、続行する(例外を投げる、特殊な値を返す)の選択肢を意識して、選ぶようにする。 + * 異常終了、続行する(例外を投げる、特殊な値を返す)の選択肢を意識して、選べると良い。 * 異常終了 * エラーに気づきやすい。 https://github.com/mura0086/arai60/pull/11#discussion_r1986104852 @@ -200,7 +200,6 @@ var isValid = function(characters) { // } ``` - * ボトルネックではないところは、最適化しない。 ## その他の解法 @@ -234,8 +233,7 @@ var isValid = function(characters) { * `*2` 番兵をおく方法 - * 番兵をおく方法がる. (`*2`) - * 以下の方法でさらに簡潔にかける. + * 以下の方法でさらに簡潔にかける. ``` const bracket_pairs = new Map([ ["{", "}"], From cbea1b20f45e8f27d44be040f2637722b22d7a1c Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Wed, 9 Apr 2025 12:57:36 +0900 Subject: [PATCH 07/17] feat : #10 add unnecessary comment in "20. Valid Parentheses" --- stack/valid-parentheses/answer.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 6d20768..826e3c4 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -179,11 +179,16 @@ var isValid = function(characters) { * 異常終了、続行する(例外を投げる、特殊な値を返す)の選択肢を意識して、選べると良い。 * 異常終了 * エラーに気づきやすい。 https://github.com/mura0086/arai60/pull/11#discussion_r1986104852 + * 重要なエラー + * 続行 + * 重要でないエラー 参考 : https://discord.com/channels/1084280443945353267/1226508154833993788/1227171332131786774 * 異常な入力への対応を頭に入れてコードを書く。 - * 質問されたら、結果と選択肢と理由を回答できる状態にする。 https://discord.com/channels/1084280443945353267/1316770883729100810/1335077966954369095 + 参考 : https://discord.com/channels/1084280443945353267/1316770883729100810/1335077966954369095 + + * 質問されたら、結果と選択肢と理由を回答できる状態にする。 * lilnoahhhのPR https://github.com/lilnoahhh/leetcode/pull/7 * Stringで判定する方法がある。 From 96aa98bac1d9753d2d3275b639f31cc7ac84b3bc Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Thu, 17 Apr 2025 09:21:54 +0900 Subject: [PATCH 08/17] feat : #10 update the comment --- stack/valid-parentheses/answer.md | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 826e3c4..df983e3 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -132,13 +132,9 @@ var isValid = function(characters) { ``` ## 感想 -### 一番ためになったこと - -* 不正な入力に対して、エラーハンドリングをどうするかという視点がなかった。 - https://github.com/lilnoahhh/leetcode/pull/7#discussion_r1948110757 - ### 他の人のコードを読んで + * BumbuShoji のPR https://github.com/BumbuShoji/Leetcode/pull/7 * 開き括弧と閉じ括弧の対応関係を表すMapを用意することも可能。 (`*1`で解法を追加) @@ -172,8 +168,10 @@ var isValid = function(characters) { ``` * Odaのコメント https://discord.com/channels/1084280443945353267/1225849404037009609/1231646037077131315 - * エラーハンドリングの観点を持っていなかった。 - > open_to_close でデータは持ちたいです。文字列にカッコ以外が来たときに落ちないで欲しいからです。 + * 不正な入力に対して、エラーハンドリングをどうするかという視点がなかった。 + https://github.com/lilnoahhh/leetcode/pull/7#discussion_r1948110757 + + > open_to_close でデータは持ちたいです。文字列にカッコ以外が来たときに落ちないで欲しいからです。 * 括弧以外の文字が入ってきた際のあるべきは、エラーログを吐き、exit 1で異常終了することと考えた。 https://discord.com/channels/1084280443945353267/1225849404037009609/1231648833914802267 * 異常終了、続行する(例外を投げる、特殊な値を返す)の選択肢を意識して、選べると良い。 @@ -206,6 +204,10 @@ var isValid = function(characters) { } ``` + +* SanakoMeine のPR https://github.com/SanakoMeine/leetcode/pull/7 + * 簡潔で読みやすい. + ## その他の解法 * `*1` 括弧の対応関係を表すMapを使う方法 @@ -233,9 +235,6 @@ var isValid = function(characters) { }; ``` -* SanakoMeine のPR https://github.com/SanakoMeine/leetcode/pull/7 - * 以下のコードが簡潔で読みやすい. - * `*2` 番兵をおく方法 * 以下の方法でさらに簡潔にかける. @@ -250,7 +249,6 @@ var isValid = function(characters) { ``` ``` - function doesMatchBracket(candidate, close_bracket_character) { if (candidate === "(" && close_bracket_character === ")") { return true From 38c934f1ed8975c73fec94350b5ded271199a8f1 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Apr 2025 11:07:05 +0900 Subject: [PATCH 09/17] feat : #10 invalid input error handling --- stack/valid-parentheses/answer.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index df983e3..2e606ab 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -280,3 +280,32 @@ var isValid = function(characters) { return container.length === 1 && container[container.length-1] === "SENTINEL" }; ``` + +* `*3` 不正な入力のエラーハンドリングを行う解法 + +``` +const isValid = function(characters) { + const open_to_close = new Map([ + ["{", "}"], + ["[", "]"], + ["(", ")"] + ]) + const container = [] + const expected_characters = ["(", ")", "{", "}", "[", "]"] + for (const character of characters) { + if (!expected_characters.includes(character)) { + throw new Error(`Invalid character. Expected : ${expected_character.join(",")}. Current : ${character}`) // UPDATE + } + if (open_to_close.has(character)) { + container.push(character) + continue + } + const expected_close_bracket = open_to_close.get(container.pop()) + const close_bracket_character = character + if (expected_close_bracket !== close_bracket_character) { + return false + } + } + return container.length === 0 +}; +``` From 06b0afe9b05428e2d1209d0993551cade240b58e Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Apr 2025 11:08:51 +0900 Subject: [PATCH 10/17] feat : #10 add the javascript comment in each script --- stack/valid-parentheses/answer.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 2e606ab..14ed53f 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -11,7 +11,7 @@ * 作業が完了して、机の上に何も残っていない場合には、終了する。 * 机の上に何か残っている場合には、異常を報告する。 -``` +```javascript var isValid = function(characters) { const container = [] const open_bracket_chars = ["(", "{", "["] @@ -49,7 +49,7 @@ var isValid = function(characters) { ### やったこと * 関数を分けた. -``` +```javascript function doesMatchBracket(candidate, close_bracket) { if (candidate === "[" && close_bracket === "]") { return true @@ -91,7 +91,7 @@ var isValid = function(characters) { ## STEP 3 -``` +```javascript /** * @param {string} s * @return {boolean} @@ -212,7 +212,7 @@ var isValid = function(characters) { * `*1` 括弧の対応関係を表すMapを使う方法 -``` +```javascript var isValid = function(characters) { const open_to_close = new Map() open_to_close.set("(", ")") @@ -248,7 +248,7 @@ var isValid = function(characters) { const container = ["*"] ``` -``` +```javascript function doesMatchBracket(candidate, close_bracket_character) { if (candidate === "(" && close_bracket_character === ")") { return true @@ -282,8 +282,9 @@ var isValid = function(characters) { ``` * `*3` 不正な入力のエラーハンドリングを行う解法 + * 正常終了(エラーログ)と異常終了の選択肢のうち、異常終了を選択する。 -``` +```javascript const isValid = function(characters) { const open_to_close = new Map([ ["{", "}"], From 807fd0d7db4ae9d01f3f7de3b9e477c6e52a97e2 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Apr 2025 11:12:28 +0900 Subject: [PATCH 11/17] feat : #10 add the javascript code in each script --- stack/valid-parentheses/answer.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 14ed53f..4998287 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -142,19 +142,19 @@ var isValid = function(characters) { * 配列が要素数0の時に、pop()で、undefinedを返すため、たまたま上手く行った。 参考 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop#return_value * 変更前 - ``` + ```javascript const open_bracket_candidate = container.pop() ``` * 変更後 - ``` + ```javascript const open_bracket_candidate = container.pop() || "" const open_bracket_candidate = container.length > 0 ? container.pop() : "" - ``` + ``` * 最後のif文は、`return container.length === 0`、`return !arr.length` でも良い * 変更前 - ``` + ```javascript if (container.length === 0) { return true } @@ -163,7 +163,7 @@ var isValid = function(characters) { * 変更後 - ``` + ```javascript return container.length === 0 ``` @@ -191,12 +191,13 @@ var isValid = function(characters) { * lilnoahhhのPR https://github.com/lilnoahhh/leetcode/pull/7 * Stringで判定する方法がある。 * 変更前 - ``` + ```javascript const open_bracket_chars = ["(", "{", "["] if (open_bracket_chars.includes(character)) { // } ``` + * 変更後 ``` const open_brackets = "{([" if (open_brackets.includes(character)) { From f9ece5c012b2ca45eedee5b4fa75aa947aa21cc6 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Apr 2025 11:13:43 +0900 Subject: [PATCH 12/17] feat : #10 add the javascript code in each script --- stack/valid-parentheses/answer.md | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 4998287..c0b8962 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -129,7 +129,6 @@ var isValid = function(characters) { } return false }; -``` ## 感想 ### 他の人のコードを読んで @@ -142,14 +141,17 @@ var isValid = function(characters) { * 配列が要素数0の時に、pop()で、undefinedを返すため、たまたま上手く行った。 参考 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop#return_value * 変更前 - ```javascript - const open_bracket_candidate = container.pop() - ``` + + ```javascript + const open_bracket_candidate = container.pop() + ``` + * 変更後 - ```javascript - const open_bracket_candidate = container.pop() || "" - const open_bracket_candidate = container.length > 0 ? container.pop() : "" - ``` + + ```javascript + const open_bracket_candidate = container.pop() || "" + const open_bracket_candidate = container.length > 0 ? container.pop() : "" + ``` * 最後のif文は、`return container.length === 0`、`return !arr.length` でも良い * 変更前 @@ -191,14 +193,17 @@ var isValid = function(characters) { * lilnoahhhのPR https://github.com/lilnoahhh/leetcode/pull/7 * Stringで判定する方法がある。 * 変更前 + ```javascript const open_bracket_chars = ["(", "{", "["] if (open_bracket_chars.includes(character)) { // } ``` + * 変更後 - ``` + + ```javascript const open_brackets = "{([" if (open_brackets.includes(character)) { // From 18dbce1216052df5ee0c6b20ea310eb21589949d Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Apr 2025 11:14:46 +0900 Subject: [PATCH 13/17] feat : #10 add the javascript code in each script --- stack/valid-parentheses/answer.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index c0b8962..d12952e 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -129,6 +129,8 @@ var isValid = function(characters) { } return false }; +``` + ## 感想 ### 他の人のコードを読んで From 6c05c9f2c229410c994d8c08dac2538648becff6 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Apr 2025 11:16:28 +0900 Subject: [PATCH 14/17] feat : #10 add the javascript code in each script --- stack/valid-parentheses/answer.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index d12952e..4681880 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -144,16 +144,16 @@ var isValid = function(characters) { 参考 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop#return_value * 変更前 - ```javascript - const open_bracket_candidate = container.pop() - ``` +```javascript + const open_bracket_candidate = container.pop() +``` * 変更後 - ```javascript - const open_bracket_candidate = container.pop() || "" - const open_bracket_candidate = container.length > 0 ? container.pop() : "" - ``` +```javascript + const open_bracket_candidate = container.pop() || "" + const open_bracket_candidate = container.length > 0 ? container.pop() : "" +``` * 最後のif文は、`return container.length === 0`、`return !arr.length` でも良い * 変更前 From cad2d1cca22624b89ac94462962a2edfbca3fe8b Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Apr 2025 11:17:56 +0900 Subject: [PATCH 15/17] feat : #10 add the javascript code in each script --- stack/valid-parentheses/answer.md | 61 ++++++++++++++++--------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 4681880..42ec6d4 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -158,18 +158,18 @@ var isValid = function(characters) { * 最後のif文は、`return container.length === 0`、`return !arr.length` でも良い * 変更前 - ```javascript - if (container.length === 0) { - return true - } - return false - ``` +```javascript + if (container.length === 0) { + return true + } + return false +``` * 変更後 - ```javascript - return container.length === 0 - ``` +```javascript + return container.length === 0 +``` * Odaのコメント https://discord.com/channels/1084280443945353267/1225849404037009609/1231646037077131315 * 不正な入力に対して、エラーハンドリングをどうするかという視点がなかった。 @@ -196,21 +196,21 @@ var isValid = function(characters) { * Stringで判定する方法がある。 * 変更前 - ```javascript - const open_bracket_chars = ["(", "{", "["] - if (open_bracket_chars.includes(character)) { - // - } - ``` +```javascript + const open_bracket_chars = ["(", "{", "["] + if (open_bracket_chars.includes(character)) { + // + } +``` * 変更後 - ```javascript - const open_brackets = "{([" - if (open_brackets.includes(character)) { - // - } - ``` +```javascript + const open_brackets = "{([" + if (open_brackets.includes(character)) { + // + } +``` * SanakoMeine のPR https://github.com/SanakoMeine/leetcode/pull/7 @@ -246,15 +246,16 @@ var isValid = function(characters) { * `*2` 番兵をおく方法 * 以下の方法でさらに簡潔にかける. - ``` - const bracket_pairs = new Map([ - ["{", "}"], - ["(", ")"], - ["[", "]"], - ["*", ""], - ]) - const container = ["*"] - ``` + +```javascript + const bracket_pairs = new Map([ + ["{", "}"], + ["(", ")"], + ["[", "]"], + ["*", ""], + ]) + const container = ["*"] +``` ```javascript function doesMatchBracket(candidate, close_bracket_character) { From ee3aaa56e31d3098bbe5416b717471197357ed0c Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Apr 2025 11:23:51 +0900 Subject: [PATCH 16/17] feat : #10 add the code after the comment(handle pop() functionwith empty array) is received --- stack/valid-parentheses/answer.md | 59 +++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 42ec6d4..395f3a6 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -319,3 +319,62 @@ const isValid = function(characters) { return container.length === 0 }; ``` + +### レビューを受けて + +#### レビューコメント1 + +* 変更前 + +```javascript +const isValid = function(characters) { + const open_to_close = new Map([ + ["{", "}"], + ["[", "]"], + ["(", ")"] + ]) + const container = [] + const expected_characters = ["(", ")", "{", "}", "[", "]"] + for (const character of characters) { + if (open_to_close.has(character)) { + container.push(character) + continue + } + const expected_close_bracket = open_to_close.get(container.pop()) + const close_bracket_character = character + if (expected_close_bracket !== close_bracket_character) { + return false + } + } + return container.length === 0 +}; +``` + +* 変更後 (conatiner.pop()をする際にundefinedとstringの可能性を考慮する必要がある。) + +```javascript +const isValid = function(characters) { + const open_to_close = new Map([ + ["{", "}"], + ["[", "]"], + ["(", ")"] + ]) + const container = [] + const expected_characters = ["(", ")", "{", "}", "[", "]"] + for (const character of characters) { + if (open_to_close.has(character)) { + container.push(character) + continue + } + // 箱の中身が空で、閉じ括弧が挿入された際には、falseを返す。 + if (container.length === 0) { + return false + } + const expected_close_bracket = open_to_close.get(container.pop()) + const close_bracket_character = character + if (expected_close_bracket !== close_bracket_character) { + return false + } + } + return container.length === 0 +}; From 8aeb275740b436ce34cdd53ac9dcc16bc343a658 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Apr 2025 11:32:26 +0900 Subject: [PATCH 17/17] feat : #10 make the markdown clean --- stack/valid-parentheses/answer.md | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/stack/valid-parentheses/answer.md b/stack/valid-parentheses/answer.md index 395f3a6..2c2bb25 100644 --- a/stack/valid-parentheses/answer.md +++ b/stack/valid-parentheses/answer.md @@ -92,10 +92,6 @@ var isValid = function(characters) { ## STEP 3 ```javascript -/** - * @param {string} s - * @return {boolean} - */ function doesMatchBracket(candidate, close_bracket_character) { if (candidate === "(" && close_bracket_character === ")") { return true @@ -142,32 +138,30 @@ var isValid = function(characters) { * はじめに、閉じ括弧があるケースを想定できていなかった。 * 配列が要素数0の時に、pop()で、undefinedを返すため、たまたま上手く行った。 参考 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop#return_value - * 変更前 ```javascript + // BEFORE const open_bracket_candidate = container.pop() ``` - * 変更後 - ```javascript + // AFTER const open_bracket_candidate = container.pop() || "" const open_bracket_candidate = container.length > 0 ? container.pop() : "" ``` * 最後のif文は、`return container.length === 0`、`return !arr.length` でも良い - * 変更前 ```javascript + // BEFORE if (container.length === 0) { return true } return false ``` - * 変更後 - ```javascript + // AFTER return container.length === 0 ``` @@ -194,18 +188,17 @@ var isValid = function(characters) { * lilnoahhhのPR https://github.com/lilnoahhh/leetcode/pull/7 * Stringで判定する方法がある。 - * 変更前 ```javascript + // BEFORE const open_bracket_chars = ["(", "{", "["] if (open_bracket_chars.includes(character)) { // } ``` - * 変更後 - ```javascript + // AFTER const open_brackets = "{([" if (open_brackets.includes(character)) { //