-
Notifications
You must be signed in to change notification settings - Fork 0
0020. valid parentheses #3
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
stack.pop() | ||
else: | ||
return False | ||
if len(stack) != 0: |
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.
return not stack
のほうがシンプルだと思います。
if bracket == "(" or bracket == "[" or bracket == "{": | ||
stack.append(bracket) | ||
else: | ||
if bracket == ")" and stack[-1] == "(": |
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 のように、括弧と閉じかっこの関係を dict に入れて処理したほうがシンプルだと思います。
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
stack = [] | ||
closeToOpen = { ")" : "(", "]" : "[", "}" : "{"} |
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.
変数名は 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
early return してネストを下げたほうが見やすいかもと思ったのですが、あまり変わらないかもしれません…。
問題: 20. Valid Parentheses
https://leetcode.com/problems/valid-parentheses/description/
言語: Python