-
Notifications
You must be signed in to change notification settings - Fork 0
22. Generate Parentheses #47
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
| # @return {String[]} | ||
| def generate_parenthesis(n) | ||
| result = [] | ||
| generate_parenthesis_helper = lambda do |left, right, 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.
個人的には num_left のように個数であることがわかるように書きたいなと思いました。
| # @return {String[]} | ||
| def generate_parenthesis(n) | ||
| combinations = [] | ||
| generate_parenthesis_helper = lambda do |parenthesis, left ,right| |
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.
left, right とすることをお勧めいたします。
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.
コメントありがとうございます。
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
endThere 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.
単純にスペースをどこに置くかというスタイルの問題じゃないですかね?
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.
ああ、そういうことでしたか、ありがとうございます。
| end | ||
| end | ||
| generate_parenthesis_helper.call(0, 0, []) | ||
| result |
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.
読みやすいです。自分もleft, rightはindexのイメージがあるのでnum_left, num_rightの方がいいかもしれないです。num_open, num_closeでもいいかもしれません。
解いた問題
22. Generate Parentheses
使用言語
Ruby
次に解く問題
703. Kth Largest Element in a Stream