Skip to content

Commit 105b2fc

Browse files
committed
Time: 54 ms (45.93%), Space: 16.6 MB (80.15%) - LeetHub
1 parent 6ef1990 commit 105b2fc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def convert(self, s: str, numRows: int) -> str:
3+
if numRows == 1 or numRows >= len(s):
4+
return s
5+
6+
# Create an array of empty strings for each row
7+
rows = [''] * numRows
8+
cur_row = 0
9+
going_down = False
10+
11+
# Traverse the string, appending characters to the appropriate row
12+
for char in s:
13+
rows[cur_row] += char
14+
# If we are at the top or bottom row, reverse the direction
15+
if cur_row == 0 or cur_row == numRows - 1:
16+
going_down = not going_down
17+
# Move to the next row
18+
cur_row += 1 if going_down else -1
19+
20+
# Concatenate all rows to get the final string
21+
return ''.join(rows)

0 commit comments

Comments
 (0)