-
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?
Conversation
| row = is_down ? row + 1 : row - 1 | ||
| end | ||
| end | ||
| chars_by_row.inject("") { |result, chars| result << chars.join } |
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.
chars_by_row.map(&:join).joinこれでいけましたっけ。
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.
コメントありがとうございます。
確かにそっちの方が簡潔で良いですね。いけました。
|
|
||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
これをdirectionというような名前の1と-1を切り替える変数にして、rowの更新をrow + directionで済ませてしまうようなコードもよく見ます。(どちらでもよいと思います)
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.
コメントありがとうございます!
その発想はなかったです。
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
0...num_rowsの範囲でループを回し、その行に来るであろうindexを一回で全て計算してしまいstringに入れていく解法もあると思います。
(自分が解いたときは多分違う配列にアクセスを切り替える頻度が少なくなるおかげでこちらの解法の方が少し実行が速かったです)
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 | ||
| strings_by_row.join | ||
| end | ||
| ``` |
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.
良いと思います。
解いた問題
6. Zigzag Conversion
使用言語
Ruby
次に解く問題
Kth Largest Element in a Stream