We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 6ef1990 commit 105b2fcCopy full SHA for 105b2fc
0006-zigzag-conversion/0006-zigzag-conversion.py
@@ -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