-
Notifications
You must be signed in to change notification settings - Fork 0
Create 20. Valid Parentheses.md #2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| 時間: 3m51s<br> | ||
| Error: 1 (continue漏れ)<br> | ||
| 時間計算量: O(N)<br> | ||
| 空間計算量: O(N)<br> | ||
| N: sの長さ<br> | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| open_to_close_bracket = {"(": ")", "{": "}", "[": "]"} | ||
| unclosed_open_brackets = [] | ||
|
|
||
| for bracket in s: | ||
| if bracket in open_to_close_bracket: | ||
| unclosed_open_brackets.append(bracket) | ||
| continue | ||
| if len(unclosed_open_brackets) == 0 or open_to_close_bracket[unclosed_open_brackets[-1]] != bracket: | ||
| return False | ||
| unclosed_open_brackets.pop() | ||
|
|
||
| return not unclosed_open_brackets | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
|
|
||
| 変数名に関して: | ||
| 今回、stackはただのpopするためだけのものなので、stackでも良いかと思いました。 | ||
| またdictに関しては何を保存しているのかがわからなくなるので、bracket_があった方が良いと思います。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| bracket_dict = {"(": ")", "{": "}", "[": "]"} | ||
| bracket_stack = [] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. open/close bracketsのどっちが入っているかわかる名前がいいかと思います。unclosed_open_bracketsとかexpected_close_bracketsなどいかがでしょうか?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。 |
||
|
|
||
| for bracket in s: | ||
| if bracket in bracket_dict.keys(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 未確認ですが、.keys()は不要かも?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. はい、.keys()は.items()と自分が混乱するので明示的に書くようにしてますが、最終的に不要ですね。 |
||
| bracket_stack.append(bracket) | ||
| continue | ||
|
|
||
| if len(bracket_stack) == 0: | ||
Mike0121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return False | ||
|
|
||
| if bracket_dict[bracket_stack[-1]] != bracket: | ||
| return False | ||
|
|
||
| bracket_stack.pop() | ||
|
|
||
| return not bracket_stack | ||
| ``` | ||
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.
dictなのは型からわかるので、不要な気がします。
open_to_close_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.
ありがとうございます。そうですね、
open_to_close_bracketの方がわかりやすいです。変数名に型を入れるのは、実際の開発でコードが長く型がわかりづらくなる場合につけると思いましたが、Leet Codeのような短いコードでは基本不要と考えて良いでしょうか?(変数名の長さに余裕があればつける程度)
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.
ここでは変数名に型を入れる必要はなさそうです。分かりやすくなるなら入れてもいいので、場合によるかと。
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.
承知しました。型を明示することにメリットがあるかを都度考えます。
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.
LeetCodeのプラットフォーム上のエディタとかは別ですか、VSCodeとかでは定義や型情報のを見れると思うので実際の開発ではそこまでメリットはないかなと思います。
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.
ありがとうございます。確かにそうですね、頭に入れておきます。