-
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
Open
akmhmgc
wants to merge
1
commit into
main
Choose a base branch
from
22
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # step1 何も見ずに解く | ||
|
|
||
| backtracking を使う。 | ||
|
|
||
| - `(`の数が n 個より少なければ`(`を 1 つつなげる | ||
| - `)`の数が`(`より少なければ`)`を 1 つつなげる | ||
|
|
||
| というパターンで分類。`)`の数が n と一致した時に parenthesis が完成するので答えに含める。 | ||
|
|
||
| ```ruby | ||
| # @param {Integer} n | ||
| # @return {String[]} | ||
| def generate_parenthesis(n) | ||
| combinations = [] | ||
| generate_parenthesis_helper = lambda do |parenthesis, left ,right| | ||
| if right == n | ||
| combinations << parenthesis.join | ||
| return | ||
| end | ||
| if left < n | ||
| parenthesis << "(" | ||
| generate_parenthesis_helper.call(parenthesis, left + 1, right) | ||
| parenthesis.pop | ||
| end | ||
| if right < left | ||
| parenthesis << ")" | ||
| generate_parenthesis_helper.call(parenthesis, left, right + 1) | ||
| parenthesis.pop | ||
| end | ||
| end | ||
| generate_parenthesis_helper.call([], 0, 0) | ||
| combinations | ||
| end | ||
| ``` | ||
|
|
||
| 他には、 | ||
| 可能なだけ左を入れる + 右を一つ入れる | ||
| という分類もできる。 | ||
|
|
||
| ```ruby | ||
| # @param {Integer} n | ||
| # @return {String[]} | ||
| def generate_parenthesis(n) | ||
| combinations = [] | ||
| generate_parenthesis = lambda do |parenthesis, left ,right| | ||
| if right == n | ||
| combinations << parenthesis.join | ||
| return | ||
| end | ||
| (1..(n - left)).each do |i| | ||
| i.times { parenthesis << "(" } | ||
| parenthesis << ")" | ||
| generate_parenthesis.call(parenthesis, left + i, right + 1) | ||
| (i + 1).times { parenthesis.pop } | ||
| end | ||
| if left > right | ||
| parenthesis << ")" | ||
| generate_parenthesis.call(parenthesis, left, right + 1) | ||
| parenthesis.pop | ||
| end | ||
| end | ||
| generate_parenthesis.call([], 0, 0) | ||
| combinations | ||
| end | ||
| ``` | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # step2 他の方の解答を見る | ||
| https://github.com/olsen-blue/Arai60/pull/54/files/a0087de7fa136c0c271a07ec29a364c67f40d849#r2022389382 | ||
|
|
||
| > はじめの括弧とそれに対応する括弧に注目して「(A)B」と分けるのも分類ですね。 | ||
|
|
||
| この考え方は全く頭になかった。 | ||
|
|
||
| ```ruby | ||
| # @param {Integer} n | ||
| # @return {String[]} | ||
| def generate_parenthesis(n) | ||
| return [""] if n == 0 | ||
|
|
||
| combinations = [] | ||
| n.times do |i| | ||
| lefts = generate_parenthesis(i) | ||
| rights = generate_parenthesis(n - i - 1) | ||
| lefts.each do |left| | ||
| rights.each do |right| | ||
| combinations << "(" + left + ")" + right | ||
| end | ||
| end | ||
| end | ||
| combinations | ||
| end | ||
| ``` | ||
|
|
||
|
|
||
| https://github.com/tokuhirat/LeetCode/pull/53/files#diff-edeaf9ab4fec143afff62d1f44e6f5d9ca2d7c345ff74d8c89f41ee178034827R81-R101 | ||
| 使った括弧の数か、残りの括弧の数か。 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # step3 3回続けて10分以内に書いてエラーを出さなければOKとする | ||
|
|
||
| ```ruby | ||
| # @param {Integer} n | ||
| # @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 commentThe reason will be displayed to describe this comment to others. Learn more. 個人的には num_left のように個数であることがわかるように書きたいなと思いました。 |
||
| if right == n | ||
| result << parenthesis.join | ||
| return | ||
| end | ||
| if left < n | ||
| parenthesis << "(" | ||
| generate_parenthesis_helper.call(left + 1, right, parenthesis) | ||
| parenthesis.pop | ||
| end | ||
| if right < left | ||
| parenthesis << ")" | ||
| generate_parenthesis_helper.call(left, right + 1, parenthesis) | ||
| parenthesis.pop | ||
| end | ||
| end | ||
| generate_parenthesis_helper.call(0, 0, []) | ||
| result | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 読みやすいです。自分もleft, rightはindexのイメージがあるのでnum_left, num_rightの方がいいかもしれないです。num_open, num_closeでもいいかもしれません。 |
||
| end | ||
| ``` | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## step4 レビューを受けて解答を修正 |
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.
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に破壊的変更を加えていて予想しにくいので、関数の引数として渡さない方が良いという考えのもとでしょうか?
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.
単純にスペースをどこに置くかというスタイルの問題じゃないですかね?
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.
ああ、そういうことでしたか、ありがとうございます。