From 6602ee7f8907a1ac3fd981ac11e5c710ec1aeba3 Mon Sep 17 00:00:00 2001 From: TakaoKoizumi Date: Sun, 19 Oct 2025 21:44:56 +0900 Subject: [PATCH 1/6] =?UTF-8?q?=E2=9C=A820.ValidParentheses:=20=E5=88=9D?= =?UTF-8?q?=E5=9B=9E=E8=AA=A4=E7=AD=94=E3=81=AE=E8=80=83=E5=AF=9F=E3=81=A8?= =?UTF-8?q?=E5=86=8D=E7=8F=BE=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20.ValidParentheses_251019.md | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/20.ValidParentheses_251019.md b/20.ValidParentheses_251019.md index e69de29..2c7a9aa 100644 --- a/20.ValidParentheses_251019.md +++ b/20.ValidParentheses_251019.md @@ -0,0 +1,60 @@ +# 1st +(1回目の誤答) +```python +class Solution(object): + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + brackets = {'(': ')', '{': '}', '[' : ']'} + close_brackets = {')','}',']'} + correct_s = {} + for i in range(len(s)): + # 開かっこかどうか + if s[i] in brackets: + correct_s[i] = s[i] + + # 閉じ括弧では何もしない + # open_orderに開き括弧の種類と配置順が入っている + {0:(,2:[)} + for i in range(len(s)): + if i in correct_s: + # 開括弧の次→閉じ括弧or開括弧の次に開括弧があれば最後の開き括弧の次が閉じ括弧で閉じ括弧は最後の開き括弧に対応する。 + # 考慮する条件が多すぎるのでもっとシンプルな対応を確認する数え方がないか + + # correct_sとsを比較する。 + + [()] + 0 1 2 + + +``` + +・後方の開かっこから順に、その開きかっこに対応する閉じ括弧が入っていればOK→スタックの問題 +・正直、close_to_openにする理由がよくわからなかった。過去のレビューを見てもopen_to_closeが推奨されているので、こちらで回答する。 +・open_to_closeの答えはまだ見ていない。 + + +(2回目で正答) +```python +class Solution(object): + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + open_brackets_stack = [] + open_to_close = {'(': ')', '{': '}', '[' : ']'} + correct_s = '' + for char in s: + if char in open_to_close: + open_brackets_stack.append(char) + correct_s = correct_s + char + else: + + correct_s = correct_s + open_to_close[open_brackets_stack.pop()] if open_brackets_stack else "#" + return s == correct_s and len(open_brackets_stack) == 0 + +``` + From 9e79e39a6e7d38aad1ab6876f6ae1e1392886dc6 Mon Sep 17 00:00:00 2001 From: TakaoKoizumi Date: Sun, 19 Oct 2025 21:46:12 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=94=A820.ValidParentheses:=20?= =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E7=A9=BA=E7=99=BD=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20.ValidParentheses_251019.md | 1 - 1 file changed, 1 deletion(-) diff --git a/20.ValidParentheses_251019.md b/20.ValidParentheses_251019.md index 2c7a9aa..060e0f0 100644 --- a/20.ValidParentheses_251019.md +++ b/20.ValidParentheses_251019.md @@ -52,7 +52,6 @@ class Solution(object): open_brackets_stack.append(char) correct_s = correct_s + char else: - correct_s = correct_s + open_to_close[open_brackets_stack.pop()] if open_brackets_stack else "#" return s == correct_s and len(open_brackets_stack) == 0 From e2152ae9c14ff4fbb8fe9533382605c1c77ea938 Mon Sep 17 00:00:00 2001 From: TakaoKoizumi Date: Sun, 19 Oct 2025 22:01:08 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=E2=9C=A820.ValidParentheses:=20AI=E3=83=AC?= =?UTF-8?q?=E3=83=93=E3=83=A5=E3=83=BC=E6=8C=87=E6=91=98=EF=BC=88=E4=B8=89?= =?UTF-8?q?=E9=A0=85=E6=BC=94=E7=AE=97=E5=AD=90=E3=81=AE=E5=84=AA=E5=85=88?= =?UTF-8?q?=E5=BA=A6=E3=81=A8=E5=95=8F=E9=A1=8C=E7=82=B9=EF=BC=89=E3=81=A8?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=89=88=E3=82=B3=E3=83=BC=E3=83=89=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E8=A8=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20.ValidParentheses_251019.md | 41 ++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/20.ValidParentheses_251019.md b/20.ValidParentheses_251019.md index 060e0f0..cab02c4 100644 --- a/20.ValidParentheses_251019.md +++ b/20.ValidParentheses_251019.md @@ -36,7 +36,7 @@ class Solution(object): ・open_to_closeの答えはまだ見ていない。 -(2回目で正答) +(2回目で正答、leetcodeではOK、AIではNG) ```python class Solution(object): def isValid(self, s): @@ -57,3 +57,42 @@ class Solution(object): ``` +・上記回答はAIレビューでNG箇所があったため検討 +・NGの内容は以下の通り。 +```python +# Pythonの三項演算子は「value_if_true if condition else value_if_false」で評価されるため、現在のコードは以下のように動作します: + +# 現在のコード (問題あり) +correct_s = correct_s + open_to_close[open_brackets_stack.pop()] if open_brackets_stack else "#" +# 解析結果: +correct_s = (correct_s + open_to_close[open_brackets_stack.pop()]) if open_brackets_stack else "#" +# スタックが空の場合: correct_s = "#" (置き換え、追記ではない) +# 最終的にはs == correct_sの比較で正しい結果が得られていますが、これは偶然に頼っています。意図をより明確にするため、括弧で優先度を明示することをお勧めします: +``` + +(2回目の回答の修正) +```python +class Solution(object): + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + open_brackets_stack = [] + open_to_close = {'(': ')', '{': '}', '[' : ']'} + correct_s = '' + for char in s: + if char in open_to_close: + open_brackets_stack.append(char) + correct_s = correct_s + char + else: + correct_s = correct_s + (open_to_close[open_brackets_stack.pop()] if open_brackets_stack else "#" ) + return s == correct_s and len(open_brackets_stack) == 0 +``` + + +他の方の回答を見る +- https://github.com/h1rosaka/arai60/pull/8/files +・リンクのプッシュダウンオートマトンが気になるので後で見る。 + + From 2962d1a7986eb01456c231b9ed35b004ec88c5ab Mon Sep 17 00:00:00 2001 From: TakaoKoizumi Date: Sat, 1 Nov 2025 16:22:38 +0900 Subject: [PATCH 4/6] =?UTF-8?q?=E2=9C=A820.ValidParentheses:=202nd?= =?UTF-8?q?=E8=A7=A3=E7=AD=94=E3=82=92=E8=BF=BD=E5=8A=A0=EF=BC=88=E5=8F=AF?= =?UTF-8?q?=E8=AA=AD=E6=80=A7=E5=90=91=E4=B8=8A=EF=BC=89=E3=81=A8=E5=88=A5?= =?UTF-8?q?=E8=A7=A3=E3=81=AE=E5=AE=9F=E8=A3=85=E3=81=8A=E3=82=88=E3=81=B3?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E6=AF=94=E8=BC=83=E3=83=A1=E3=83=A2=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E8=A8=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20.ValidParentheses_251019.md | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/20.ValidParentheses_251019.md b/20.ValidParentheses_251019.md index cab02c4..cddc873 100644 --- a/20.ValidParentheses_251019.md +++ b/20.ValidParentheses_251019.md @@ -96,3 +96,57 @@ class Solution(object): ・リンクのプッシュダウンオートマトンが気になるので後で見る。 +## 2nd (20251101) + +(解答:これまでの解答のコードの可読性を上げた。正しい文字列になっているかチェックする方式) +``` python +class Solution(object): + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + open_brackets_stack = [] + open_to_close = {'(': ')', '{': '}', '[' : ']'} + correct_s = '' + for char in s: + if char in open_to_close: + close_brackets = open_to_close[char] + open_brackets_stack.append(close_brackets) + correct_s = correct_s + char + else: + correct_s = correct_s + open_brackets_stack.pop() if open_brackets_stack else "#" + return s == correct_s and len(open_brackets_stack) == 0 +``` + +(別解:不正な値を検出して検出されなかったパターンを正とする方式) +```python +class Solution(object): + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + open_brackets_stack = [] + open_to_close = {'(': ')', '{': '}', '[' : ']'} + for char in s: + if char in open_to_close:#charが開括弧の場合 + close_brackets = open_to_close[char] + open_brackets_stack.append(close_brackets) + else: #charが閉括弧の場合 + if len(open_brackets_stack) != 0: # 開括弧と閉括弧の混在している場合 + if open_brackets_stack.pop() != char: #不正ペアの検知 + return False + if len(s) % 2 == 1: #ペアを数的に組めない余りがいるパターン”例:({}”を除外 + return False + else: # 閉じ括弧のみの場合 + return False + if len(open_brackets_stack) == len(s): return False #開括弧だけの場合を除外 + if len(open_brackets_stack) != 0: return False #開括弧と閉括弧の混在していて、開括弧と閉括弧の数が等しくない場合"[]]]"を除外 + return True +``` + +``` +別解の方がパフォーマンスが良かった。3ms。これまでの解答は10ms。 +旧解答は最後まで処理をしないと判断できないからパフォーマンスが悪いのかもしれません。 +``` From 70f70835063a9ac3edac77435f52077d0a0996e3 Mon Sep 17 00:00:00 2001 From: TakaoKoizumi Date: Sat, 1 Nov 2025 16:22:51 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=94=A820.ValidParentheses:=20?= =?UTF-8?q?=E5=85=A8=E4=BD=93=E3=81=AE=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C=E3=83=A1=E3=83=A2=E3=82=92=E8=BF=BD=E8=A8=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20.ValidParentheses_251019.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/20.ValidParentheses_251019.md b/20.ValidParentheses_251019.md index cddc873..97513ea 100644 --- a/20.ValidParentheses_251019.md +++ b/20.ValidParentheses_251019.md @@ -150,3 +150,6 @@ class Solution(object): 別解の方がパフォーマンスが良かった。3ms。これまでの解答は10ms。 旧解答は最後まで処理をしないと判断できないからパフォーマンスが悪いのかもしれません。 ``` + +・全体のレビュー対応はこれから +() \ No newline at end of file From 48bea348d54ad80ba03134010ab175c8bd1c5325 Mon Sep 17 00:00:00 2001 From: TakaoKoizumi Date: Sat, 1 Nov 2025 17:03:37 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E2=9C=A820.ValidParentheses:=20=E3=83=AC?= =?UTF-8?q?=E3=83=93=E3=83=A5=E3=83=BC=E5=AF=BE=E5=BF=9C=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E8=A8=98=EF=BC=88=E5=88=A5=E8=A7=A3=E3=82=A2=E3=83=97=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=81=E3=80=81=E6=89=80=E8=A6=81=E6=99=82=E9=96=93?= =?UTF-8?q?=E3=83=A1=E3=83=A2=E3=80=81Google=E3=82=B3=E3=83=BC=E3=83=87?= =?UTF-8?q?=E3=82=A3=E3=83=B3=E3=82=B0=E8=A6=8F=E7=B4=84=E3=81=AB=E5=9F=BA?= =?UTF-8?q?=E3=81=A5=E3=81=8F=E5=8F=AF=E8=AA=AD=E6=80=A7=E6=94=B9=E5=96=84?= =?UTF-8?q?=E6=A1=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20.ValidParentheses_251019.md | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/20.ValidParentheses_251019.md b/20.ValidParentheses_251019.md index 97513ea..cf5e0f4 100644 --- a/20.ValidParentheses_251019.md +++ b/20.ValidParentheses_251019.md @@ -119,7 +119,10 @@ class Solution(object): return s == correct_s and len(open_brackets_stack) == 0 ``` +以下、レビュー対応 +その1:アプローチを変える (別解:不正な値を検出して検出されなかったパターンを正とする方式) +・正答判定を出すまで1時間前後かかってしまった。 ```python class Solution(object): def isValid(self, s): @@ -150,6 +153,29 @@ class Solution(object): 別解の方がパフォーマンスが良かった。3ms。これまでの解答は10ms。 旧解答は最後まで処理をしないと判断できないからパフォーマンスが悪いのかもしれません。 ``` +その2:Googleのコーディング規約を参考にする。1行を短くする。 +(コードの可読性:) +```python +class Solution: + def isValid(self, s): + """ + :type s: str + :rtype: bool + """ + open_brackets_stack = [] + open_to_close = {'(': ')', '{': '}', '[' : ']'} + correct_s = '' + for char in s: + if char in open_to_close: + close_brackets = open_to_close[char] + open_brackets_stack.append(close_brackets) + correct_s = correct_s + char + else: + if open_brackets_stack: + correct_s = correct_s + open_brackets_stack.pop() + else: "#" + return s == correct_s and not open_brackets_stack + +``` -・全体のレビュー対応はこれから -() \ No newline at end of file +・他レビューは順次対応 \ No newline at end of file