-
Notifications
You must be signed in to change notification settings - Fork 0
20. Valid Parentheses #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| if len(s) <= 1: | ||
| return False | ||
|
|
||
| visited_chars = deque() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(step2,3含め)dequeではなくlistでも良いのかなと思いました。(popとappendしかしてなさそうなため。)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コードの動きの上では変わらないと思いますが、適切なコンテナを使ったほうが意図が伝わりやすいか思います。
| if actual_bracket != expected_bracket: | ||
| return False | ||
| else: | ||
| unmatched_open_brackets.append(bracket) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(好みの問題かもしれませんが)、ひだりカッコの処理が先にあったくれた方が個人的には頭に入りやすいかもと思いました。(左かっこの方がシンプルなので、シンプルなものが先に片付いてくれて方が、最後に残っているより少し楽に感じるため)
| ```python | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| closing_to_opening_brackets = {")": "(", "]": "[", "}": "{"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
全体的に読みやすいと思います!個人的には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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
93行目の処理を先に書いたほうが読みやすいと思いました。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.9kpbwslvv3yv
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 です
| closing_to_opening_bracket = {")": "(", "]": "[", "}": "{"} | ||
| unmatched_brackets = deque() | ||
|
|
||
| for bracket in s: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
問題の制約で起きえないですが、文字列にかっこ以外の文字が含まれる可能性を考えると、bracketとして良いのだろうか、という気持ちになりました。
| unmatched_brackets = deque() | ||
|
|
||
| for bracket in s: | ||
| if bracket in closing_to_opening_bracket: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 です
| if len(s) <= 1: | ||
| return False | ||
|
|
||
| visited_chars = deque() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deque である必要はないというのはその通りでして、追加すると collections から import しているというのは明記したいですね (LeetCode だとデフォルトで import されているので)。
20. Valid Parentheses
問題リンク: https://leetcode.com/problems/valid-parentheses/
言語: Python
次回予告: 35. Search Insert Position