Skip to content

6. Zigzag Conversion#71

Open
hayashi-ay wants to merge 6 commits intomainfrom
hayashi-ay-patch-60
Open

6. Zigzag Conversion#71
hayashi-ay wants to merge 6 commits intomainfrom
hayashi-ay-patch-60

Conversation

@hayashi-ay
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@nodchip nodchip left a comment

Choose a reason for hiding this comment

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

よいと思います。

@nodchip
Copy link
Copy Markdown

nodchip commented Apr 5, 2024

挑戦する必要はないのですが、空間計算量 O(1) で書けますか?

if total_chars >= num_chars:
return num_cols
# 斜め右上方向に上に上がる
for _ in range(num_rows - 2, 0, -1):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここのループって、数式で書けませんか?

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.

書けそうです。動かしてないですが以下みたいな感じでいけそうな気がします。
max(0, num_rows - 2)は変数に切り出しても良いと思います。いい名前が思いつかないですが、upper_right_lenとかですかね。

total_chars += max(0, num_rows - 2)
num_cols += max(0, num_rows - 2)
if total_chars >= num_chars:
   num_cols -= (total_chars - num_chars)
   return num_cols

zigzag = to_zigzag(s, numRows)
converted = ""
for zigzag_row in zigzag:
converted += "".join(zigzag_row)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一応、文字列の追記は再構築されます。ただし、CPython ならば高速化される場合があるようです。

converted = ""
for zigzag_row in zigzag:
converted += "".join(zigzag_row)
return converted
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

return "".join(c for c in row for row in rows)みたいな感じで書けませんか?(forループの順番は間違っているかもしれないですが…)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

やっぱり反対でした。"".join(char for row in rows for char in row)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants