Skip to content

6. Zigzag Conversion - #52

Open
akmhmgc wants to merge 1 commit into
mainfrom
6
Open

6. Zigzag Conversion#52
akmhmgc wants to merge 1 commit into
mainfrom
6

Conversation

@akmhmgc

@akmhmgc akmhmgc commented Oct 11, 2025

Copy link
Copy Markdown
Owner

解いた問題

6. Zigzag Conversion

使用言語

Ruby

次に解く問題

Kth Largest Element in a Stream

@akmhmgc akmhmgc added the ruby label Oct 11, 2025
Comment thread 6/step1.md
row = is_down ? row + 1 : row - 1
end
end
chars_by_row.inject("") { |result, chars| result << chars.join }

Copy link
Copy Markdown

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

これでいけましたっけ。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。
確かにそっちの方が簡潔で良いですね。いけました。

Comment thread 6/step3.md

strings_by_row = Array.new(num_rows) { "" }
row = 0
is_downward = true

Copy link
Copy Markdown

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で済ませてしまうようなコードもよく見ます。(どちらでもよいと思います)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます!
その発想はなかったです。

Comment thread 6/step3.md
row = is_downward ? row + 1 : row - 1
end
strings_by_row.join
end

Copy link
Copy Markdown

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に入れていく解法もあると思います。
(自分が解いたときは多分違う配列にアクセスを切り替える頻度が少なくなるおかげでこちらの解法の方が少し実行が速かったです)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。
それぞれの行に来る文字のインデックスには規則性があるので、それを利用する感じですよね。

Comment thread 6/step3.md
end
strings_by_row.join
end
```

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良いと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants