Skip to content

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

0020. valid parentheses #3

wants to merge 13 commits into from

Conversation

ryo-devz
Copy link
Owner

問題: 20. Valid Parentheses

https://leetcode.com/problems/valid-parentheses/description/

言語: Python

stack.pop()
else:
return False
if len(stack) != 0:
Copy link

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] == "(":
Copy link

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 = { ")" : "(", "]" : "[", "}" : "{"}
Copy link

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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

early return してネストを下げたほうが見やすいかもと思ったのですが、あまり変わらないかもしれません…。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants