Conversation
tokuhirat
reviewed
Oct 7, 2025
| # @return {String[]} | ||
| def generate_parenthesis(n) | ||
| result = [] | ||
| generate_parenthesis_helper = lambda do |left, right, parenthesis| |
There was a problem hiding this comment.
個人的には num_left のように個数であることがわかるように書きたいなと思いました。
nodchip
reviewed
Oct 8, 2025
| # @return {String[]} | ||
| def generate_parenthesis(n) | ||
| combinations = [] | ||
| generate_parenthesis_helper = lambda do |parenthesis, left ,right| |
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます。
parenthesisは引数に渡さずに再帰関数の外側に置くということですかね。
これは、parenthesisに破壊的変更を加えていて予想しにくいので、関数の引数として渡さない方が良いという考えのもとでしょうか?
# @param {Integer} n
# @return {String[]}
def generate_parenthesis(n)
combinations = []
parenthesis = []
generate_parenthesis_helper = lambda do |left ,right|
if right == n
combinations << parenthesis.join
return
end
if left < n
parenthesis << "("
generate_parenthesis_helper.call(left + 1, right)
parenthesis.pop
end
if right < left
parenthesis << ")"
generate_parenthesis_helper.call(left, right + 1)
parenthesis.pop
end
end
generate_parenthesis_helper.call(0, 0)
combinations
end
Owner
Author
There was a problem hiding this comment.
ああ、そういうことでしたか、ありがとうございます。
potrue
reviewed
Oct 10, 2025
| end | ||
| end | ||
| generate_parenthesis_helper.call(0, 0, []) | ||
| result |
There was a problem hiding this comment.
読みやすいです。自分もleft, rightはindexのイメージがあるのでnum_left, num_rightの方がいいかもしれないです。num_open, num_closeでもいいかもしれません。
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.
解いた問題
22. Generate Parentheses
使用言語
Ruby
次に解く問題
703. Kth Largest Element in a Stream