Conversation
frinfo702
reviewed
Nov 27, 2024
frinfo702
left a comment
There was a problem hiding this comment.
該当資料が充実していて、コードも読みやすいし指摘するところが思いつかないので敢えていうなら辞書部分をリストにした解法とかどうですか
open_brackets = [...]
close_brackets = [...]みたいな感じで
Comment on lines
+71
to
+82
| open_bracket_stack = [] | ||
| for bracket in s: | ||
| if bracket in open_to_close: | ||
| open_bracket_stack.append(bracket) | ||
| continue | ||
|
|
||
| if (not open_bracket_stack \ | ||
| or open_to_close[open_bracket_stack[-1]] != bracket): | ||
| return False | ||
|
|
||
| open_bracket_stack.pop() | ||
| return not open_bracket_stack |
There was a problem hiding this comment.
Suggested change
| open_bracket_stack = [] | |
| for bracket in s: | |
| if bracket in open_to_close: | |
| open_bracket_stack.append(bracket) | |
| continue | |
| if (not open_bracket_stack \ | |
| or open_to_close[open_bracket_stack[-1]] != bracket): | |
| return False | |
| open_bracket_stack.pop() | |
| return not open_bracket_stack | |
| stack = [] | |
| for bracket in s: | |
| if bracket in open_to_close: | |
| stack.append(bracket) | |
| continue | |
| elif not stack or open_to_close[stack.pop()] != bracket: | |
| return False | |
| return not stack |
popと条件分岐を同時にできるのでこのような書き方もありかなと思いました。
hayashi-ay
reviewed
Nov 29, 2024
| if bracket in open_to_close: | ||
| open_bracket_stack.append(bracket) | ||
| continue | ||
| if (open_bracket_stack |
oda
reviewed
Dec 1, 2024
| ``` | ||
|
|
||
| - `open_bracket_stack`という名前はopen_bracketしか入らないこと・初期化時にリストを代入しているが用法としてはstackとして使うことがわかるようにした。 | ||
| - `or open_to_close[open_bracket_stack[-1]] != bracket):`はインデントして、下の部分が条件式ではないことを明確にした。[PEP8](https://peps.python.org/pep-0008/#indentation)では特に指定がなかった。 |
There was a problem hiding this comment.
こういうフォーマットは、それなりに幅があって正解がないものであるということです。
autoformatter たとえば black などを参考にするのも一つでしょう。
https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#how-black-wraps-lines
|
いいと思います! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
https://leetcode.com/problems/valid-parentheses/