Conversation
| if len(s) <= 1: | ||
| return False | ||
|
|
||
| visited_chars = deque() |
There was a problem hiding this comment.
(step2,3含め)dequeではなくlistでも良いのかなと思いました。(popとappendしかしてなさそうなため。)
There was a problem hiding this comment.
コードの動きの上では変わらないと思いますが、適切なコンテナを使ったほうが意図が伝わりやすいか思います。
| if actual_bracket != expected_bracket: | ||
| return False | ||
| else: | ||
| unmatched_open_brackets.append(bracket) |
There was a problem hiding this comment.
(好みの問題かもしれませんが)、ひだりカッコの処理が先にあったくれた方が個人的には頭に入りやすいかもと思いました。(左かっこの方がシンプルなので、シンプルなものが先に片付いてくれて方が、最後に残っているより少し楽に感じるため)
| ```python | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| closing_to_opening_brackets = {")": "(", "]": "[", "}": "{"} |
There was a problem hiding this comment.
opening_to_closing_brackets = {"(": ")", "[": "]", "{": "}"}と、開きカッコを先に持ってきたほうが読みやすいと思います。ロジックが少し変わりますが、過去のコードレビューを参照すれば見つかると思います。
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| closing_to_opening_bracket = {")": "(", "]": "[", "}": "{"} | ||
| unmatched_brackets = deque() |
There was a problem hiding this comment.
全体的に読みやすいと思います!個人的にはstep2のようにunmatched_open_bracketsと明示してくれた方が読むのが楽に思いました。
| unmatched_brackets = deque() | ||
|
|
||
| for bracket in s: | ||
| if bracket in closing_to_opening_bracket: |
There was a problem hiding this comment.
93行目の処理を先に書いたほうが読みやすいと思いました。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.9kpbwslvv3yv
| closing_to_opening_bracket = {")": "(", "]": "[", "}": "{"} | ||
| unmatched_brackets = deque() | ||
|
|
||
| for bracket in s: |
There was a problem hiding this comment.
問題の制約で起きえないですが、文字列にかっこ以外の文字が含まれる可能性を考えると、bracketとして良いのだろうか、という気持ちになりました。
| unmatched_brackets = deque() | ||
|
|
||
| for bracket in s: | ||
| if bracket in closing_to_opening_bracket: |
| if len(s) <= 1: | ||
| return False | ||
|
|
||
| visited_chars = deque() |
There was a problem hiding this comment.
deque である必要はないというのはその通りでして、追加すると collections から import しているというのは明記したいですね (LeetCode だとデフォルトで import されているので)。
20. Valid Parentheses
問題リンク: https://leetcode.com/problems/valid-parentheses/
言語: Python
次回予告: 35. Search Insert Position