From ca9d78fb1845d774d117d95b0da35f164de3ee51 Mon Sep 17 00:00:00 2001 From: mt2324 <63892273+mt2324@users.noreply.github.com> Date: Sat, 5 Sep 2026 22:40:41 +0900 Subject: [PATCH 1/2] Create 20. Valid Parentheses.md --- .../20. Valid Parentheses.md | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 20. Valid Parentheses/20. Valid Parentheses.md diff --git a/20. Valid Parentheses/20. Valid Parentheses.md b/20. Valid Parentheses/20. Valid Parentheses.md new file mode 100644 index 0000000..adb58c9 --- /dev/null +++ b/20. Valid Parentheses/20. Valid Parentheses.md @@ -0,0 +1,62 @@ +## STEP 1 +とりあえず書いてみた。 +```python +class Solution: + def isValid(self, s: str) -> bool: + left = ['(', '{', '['] + stack = [] + for i in s: + if i in left: + stack.append(i) + continue + if stack == []: + return False + if i == ')': + if stack.pop() != '(': + return False + if i == '}': + if stack.pop() != '{': + return False + if i == ']': + if stack.pop() != '[': + return False + return stack == [] +``` + +## STEP 2 +- 辞書で対応で明示すると意図が明確だし、コードの分岐が減る。 +- 鍵を開き括弧の方にすると辞書でシンプルに存在確認できてスマート +- pythonの配列の存在内の確認はimplicit falseが一般的らしい。https://google.github.io/styleguide/pyguide.html#2144-decision +- 直接returnで真偽値を書いた方が良い + +```python +class Solution: + def isValid(self, s: str) -> bool: + open_to_close = {'(': ')', '{': '}', '[': ']'} + open_brackets = [] + for c in s: + if c in open_to_close: + open_brackets.append(c) + continue + if open_brackets and open_to_close[open_brackets.pop()] == c: + continue + return False + return not open_brackets +``` + +## STEP 3 +3回書いて練習。 +```python +class Solution: + def isValid(self, s: str) -> bool: + open_to_close = {'(': ')', '{': '}', '[': ']'} + open_brackets = [] + for c in s: + if c in open_to_close: + open_brackets.append(c) + continue + if open_brackets and open_to_close[open_brackets.pop()] == c: + continue + return False + return not open_brackets +``` From b6df00c3b335627af924ae75895537894c17efa5 Mon Sep 17 00:00:00 2001 From: mt2324 <63892273+mt2324@users.noreply.github.com> Date: Thu, 10 Sep 2026 13:14:06 +0900 Subject: [PATCH 2/2] Update 20. Valid Parentheses.md --- .../20. Valid Parentheses.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/20. Valid Parentheses/20. Valid Parentheses.md b/20. Valid Parentheses/20. Valid Parentheses.md index adb58c9..44c4c4f 100644 --- a/20. Valid Parentheses/20. Valid Parentheses.md +++ b/20. Valid Parentheses/20. Valid Parentheses.md @@ -60,3 +60,37 @@ class Solution: return False return not open_brackets ``` +ネストが深くなるのはどうかなと思って、一つのif節にしましたが指摘を考慮して別のif節に分けるとこんな感じでしょうか。 +```python +class Solution: + def isValid(self, s: str) -> bool: + open_to_close = {'(': ')', '{': '}', '[': ']'} + open_brackets = [] + for c in s: + if c in open_to_close: + open_brackets.append(c) + continue + if open_brackets: + latest_bracket = open_brackets.pop() + if open_to_close[latest_bracket] == c: + continue + return False + return not open_brackets +``` +ネストを深くせずに分けるとするならばこう書いてもいいんですが、ネストが深くなってもcontinueとreturnが混ざっていない上の方がわかりやすい気がします。 +```python +class Solution: + def isValid(self, s: str) -> bool: + open_to_close = {'(': ')', '{': '}', '[': ']'} + open_brackets = [] + for c in s: + if c in open_to_close: + open_brackets.append(c) + continue + if not open_brackets: + return False + if open_to_close[open_brackets[-1]] != c: + return False + open_brackets.pop() + return not open_brackets +```