-
Notifications
You must be signed in to change notification settings - Fork 0
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
22. Generate Parentheses #70
base: main
Are you sure you want to change the base?
Conversation
return | ||
if num_left_brackets < 0 or num_left_brackets > n: | ||
return | ||
# append left brackets |
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.
このタイミングで num_left_brackets < n
かどうか調べれば、 make_parenthesis() の呼び出し回数を減らせると思います。
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.
ありがとうございます。書き直しました。
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
all_valid_parentheses = []
partial_parenthesis = []
def make_parenthesis(index, num_left_brackets):
if index == n * 2:
if num_left_brackets == 0:
all_valid_parentheses.append("".join(partial_parenthesis))
return
if num_left_brackets > 0:
partial_parenthesis.append(')')
make_parenthesis(index + 1, num_left_brackets - 1)
partial_parenthesis.pop()
if num_left_brackets < n:
partial_parenthesis.append('(')
make_parenthesis(index + 1, num_left_brackets + 1)
partial_parenthesis.pop()
make_parenthesis(0, 0)
return all_valid_parentheses
make_parenthesis(index + 1, num_left_brackets + 1) | ||
partial_parenthesis.pop() | ||
|
||
# append right brackets |
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.
このタイミングで num_left_brackets > 0 かどうか調べれば、 make_parenthesis() の呼び出し回数を減らせると思います。
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.
よいと思います。
https://leetcode.com/problems/generate-parentheses/