Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions arai60/54-60_others/60_6_Zigzag Conversion/level_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 各行の文字列をリストに格納して、最後に結合する。
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s
rows = [[] for _ in range(numRows)]
num_row = 0
direction = 1
for c in s:
rows[num_row].append(c)
if num_row == 0: # 0行目で折り返し
direction = 1
if num_row == numRows - 1: # 最終行で折り返し
direction = -1
num_row += direction
merged_all_rows = []
for row in rows:
merged_all_rows.extend(row)
zigzag_string = "".join(merged_all_rows)
return zigzag_string
26 changes: 26 additions & 0 deletions arai60/54-60_others/60_6_Zigzag Conversion/level_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 何行目になるかは規則性があるので計算で求められる
# 2*n-2個周期で行が繰り返される
# 0 6
# 1 5 7
# 2 4 8
# 3
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s
rows = [[] for _ in range(numRows)]
cycle = 2 * numRows - 2
for i in range(len(s)):
pos = i % (cycle)
if pos < numRows: # 下りのとき
num_row = pos
else: # 上りのとき
# [最下行] - [折り返して何個目か]
# (numRows - 1) - (pos - (numRows - 1))
num_row = 2 * numRows - pos - 2
rows[num_row].append(s[i])
marged_all_rows = []
for row in rows:
marged_all_rows.extend(row)
zigzag_string = "".join(marged_all_rows)
return zigzag_string
18 changes: 18 additions & 0 deletions arai60/54-60_others/60_6_Zigzag Conversion/level_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s
rows = [[] for _ in range(numRows)]
num_row = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

num_rowとnumRowsの違いがわからないので、num_row -> row_indexとかでどうでしょう。

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.

確かにnum_rowは不親切な名前な気がしてきました。

direction = 1
for c in s:
rows[num_row].append(c)
if num_row == 0:
direction = 1
if num_row == numRows - 1:
direction = -1
num_row += direction
merged_all_rows = []
for row in rows:
merged_all_rows.extend(row)
return "".join(merged_all_rows)
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(for c in row for row in rows)みたいにできそうですね。

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.

こちらの記述で表せました。

"".join(c for row in rows for c in row)

15 changes: 15 additions & 0 deletions arai60/54-60_others/60_6_Zigzag Conversion/level_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s
rows = [[] for _ in range(numRows)]
rows_index = 0
direction = 1
for c in s:
rows[rows_index].append(c)
if rows_index == 0:
direction = 1
if rows_index == numRows - 1:
direction = -1
rows_index += direction
return "".join(c for row in rows for c in row)