Conversation
nodchip
reviewed
Aug 15, 2024
| stack.pop() | ||
| else: | ||
| return False | ||
| if len(stack) != 0: |
| if bracket == "(" or bracket == "[" or bracket == "{": | ||
| stack.append(bracket) | ||
| else: | ||
| if bracket == ")" and stack[-1] == "(": |
There was a problem hiding this comment.
step2 のように、括弧と閉じかっこの関係を dict に入れて処理したほうがシンプルだと思います。
| class Solution: | ||
| def isValid(self, s: str) -> bool: | ||
| stack = [] | ||
| closeToOpen = { ")" : "(", "]" : "[", "}" : "{"} |
There was a problem hiding this comment.
変数名は lower_camel で書くことをお勧めいたします。
https://peps.python.org/pep-0008/#function-and-variable-names
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
Variable names follow the same convention as function names.
https://google.github.io/styleguide/pyguide.html#316-naming
local_var_name
| closeToOpen = { ")" : "(", "]" : "[", "}" : "{" } | ||
|
|
||
| for c in s: | ||
| if c in closeToOpen: |
There was a problem hiding this comment.
early return してネストを下げたほうが見やすいかもと思ったのですが、あまり変わらないかもしれません…。
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.
問題: 20. Valid Parentheses
https://leetcode.com/problems/valid-parentheses/description/
言語: Python