-
Notifications
You must be signed in to change notification settings - Fork 0
6. Zigzag Conversion #52
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # step1 何も見ずに解く | ||
|
|
||
| 3行の場合であれば、 | ||
| 文字の先頭から1行目->2行目->3行目->2行目->1行目->2行目 | ||
| みたいな感じで進んでいって、各行の文字を左から見た文字を追加していけば良い。 | ||
|
|
||
| Nを文字列の長さとすると、 | ||
| 時間計算量はO(N) | ||
| 空間計算量もO(N) | ||
| Nの最大値は1000なので余裕で1秒以内に間に合う。 | ||
|
|
||
| ```ruby | ||
| # @param {String} s | ||
| # @param {Integer} num_rows | ||
| # @return {String} | ||
| def convert(s, num_rows) | ||
| return s if num_rows == 1 | ||
|
|
||
| chars_by_row = Array.new(num_rows) { [] } | ||
| row = 0 | ||
| is_down = true | ||
| s.each_char do |char| | ||
| chars_by_row[row] << char | ||
|
|
||
| if row.zero? | ||
| is_down = true | ||
| row = 1 | ||
| elsif row == num_rows - 1 | ||
| is_down = false | ||
| row = num_rows - 2 | ||
| else | ||
| row = is_down ? row + 1 : row - 1 | ||
| end | ||
| end | ||
| chars_by_row.inject("") { |result, chars| result << chars.join } | ||
| end | ||
| ``` | ||
|
|
||
| Rubyの文字列はミュータブルなので以下のように書いてもコストは変わらない。 | ||
|
|
||
| ```ruby | ||
| # @param {String} s | ||
| # @param {Integer} num_rows | ||
| # @return {String} | ||
| def convert(s, num_rows) | ||
| return s if num_rows == 1 | ||
|
|
||
| strings_by_row = Array.new(num_rows) { "" } | ||
| row = 0 | ||
| is_down = true | ||
| s.each_char do |char| | ||
| strings_by_row[row] << char | ||
|
|
||
| if row.zero? | ||
| is_down = true | ||
| row = 1 | ||
| elsif row == num_rows - 1 | ||
| is_down = false | ||
| row = num_rows - 2 | ||
| else | ||
| row = is_down ? row + 1 : row - 1 | ||
| end | ||
| end | ||
| strings_by_row.join | ||
| end | ||
| ``` | ||
|
|
||
| むしろ手元でベンチマークをとったらこっちの方がわずかにパフォーマンスがよかった。 | ||
| わかりやすさは変わらないのでどっちでもいいな。 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # step2 他の方の解答を見る | ||
| - https://github.com/olsen-blue/Arai60/pull/61 | ||
|
|
||
| step1で`is_down`としていたが、`is_going_down`の方がいいかも。`is_downward`とか。 | ||
| あとis_downの切り替えとrowの更新を同時にやらずに分けた方が見やすいな。 | ||
|
|
||
| ```ruby | ||
| # @param {String} s | ||
| # @param {Integer} num_rows | ||
| # @return {String} | ||
| def convert(s, num_rows) | ||
| return s if num_rows == 1 | ||
|
|
||
| chars_by_row = Array.new(num_rows) { [] } | ||
| row = 0 | ||
| is_downward = true | ||
| s.each_char do |char| | ||
| chars_by_row[row] << char | ||
|
|
||
| is_downward = true if row.zero? | ||
| is_downward = false if row == num_rows - 1 | ||
| row = is_downward ? row + 1 : row - 1 | ||
| end | ||
| chars_by_row.inject("") { |result, chars| result << chars.join } | ||
| end | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # step3 3回続けて10分以内に書いてエラーを出さなければOKとする | ||
|
|
||
| ```ruby | ||
| # @param {String} s | ||
| # @param {Integer} num_rows | ||
| # @return {String} | ||
| def convert(s, num_rows) | ||
| return s if num_rows == 1 | ||
|
|
||
| strings_by_row = Array.new(num_rows) { "" } | ||
| row = 0 | ||
| is_downward = true | ||
|
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. これをdirectionというような名前の1と-1を切り替える変数にして、rowの更新をrow + directionで済ませてしまうようなコードもよく見ます。(どちらでもよいと思います)
Owner
Author
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. コメントありがとうございます! |
||
| s.each_char do |char| | ||
| strings_by_row[row] << char | ||
|
|
||
| is_downward = true if row.zero? | ||
| is_downward = false if row == num_rows - 1 | ||
| row = is_downward ? row + 1 : row - 1 | ||
| end | ||
| strings_by_row.join | ||
| end | ||
|
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. 0...num_rowsの範囲でループを回し、その行に来るであろうindexを一回で全て計算してしまいstringに入れていく解法もあると思います。
Owner
Author
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. ありがとうございます。 |
||
| ``` | ||
|
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. 良いと思います。 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## step4 レビューを受けて解答を修正 |
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.
コメントありがとうございます。
確かにそっちの方が簡潔で良いですね。いけました。