Skip to content

Conversation

@akmhmgc
Copy link
Owner

@akmhmgc akmhmgc commented Oct 11, 2025

解いた問題

6. Zigzag Conversion

使用言語

Ruby

次に解く問題

Kth Largest Element in a Stream

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

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
Owner Author

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
Copy link

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
Owner Author

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
Copy link

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
Owner Author

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
```

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.

5 participants