Create 22. Generate Parentheses.md#52
Open
fuga-98 wants to merge 1 commit into
Open
Conversation
nodchip
reviewed
May 26, 2025
| if num_open >= n and num_close >= n: | ||
| result.append(parentheses) | ||
| if num_open < n: | ||
| stack.append((num_open + 1, num_close, f'{parentheses}(')) |
There was a problem hiding this comment.
文字列を連結する際、 + 演算子 1 つで済むのであれば、 + で結合しても、十分シンプルかつ読みやすいと思います。複数個になった場合は、読みやすさに応じて f 文字列に切り替えたほうがよいと思います。
Owner
Author
There was a problem hiding this comment.
レビューありがとうございます。
結合する文字列がかっこなのもあいまって、+演算子のほうが読みやすく感じました。
oda
reviewed
May 26, 2025
| - コピーの際の計算量。 | ||
| - Step1のソースはコピーが発生してますね。 | ||
| - 今回の問題では気にしなくても良さそうか? | ||
| - カタラン数は常識ではないらしい。 |
There was a problem hiding this comment.
カタラン数は聞いたことありました?
大学受験でたまに出てくることがありますね。(知らなくても解ける程度。)
Owner
Author
There was a problem hiding this comment.
| return result | ||
| ``` | ||
|
|
||
| 紙に書きながらデバッグしたらなんとなくわかった。 |
Owner
Author
There was a problem hiding this comment.
言われてみればそうでした。
計算量の観点では文字列ではなくリストで作っていって最後にjoinしたほうが良さそうです。
hroc135
reviewed
Jun 10, 2025
|
|
||
| ```python | ||
| class Solution: | ||
| def generateParenthesis(self, n: int) -> List[str]: |
There was a problem hiding this comment.
文字列を丸ごとコピーしないようにするやつを、Python にしてみました。
https://discord.com/channels/1084280443945353267/1262688866326941718/1363826801016701018
from functools import cache
from typing import List, Callable
@cache
def generate_functions_to_append_parenthesis(n: int) -> List[Callable[[List[str]], None]]:
if n == 0:
return [lambda sb: None]
result = []
for i in range(n):
for a in generate_functions_to_append_parenthesis(i):
for b in generate_functions_to_append_parenthesis(n - i - 1):
def fn(sb: List[str], a=a, b=b) -> None:
sb.append("(")
a(sb)
sb.append(")")
b(sb)
result.append(fn)
return result
def generate_parenthesis(n: int) -> List[str]:
funcs = generate_functions_to_append_parenthesis(n)
result = []
for f in funcs:
sb = []
f(sb)
result.append("".join(sb))
return result
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/generate-parentheses/description/