Skip to content
Open
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
16 changes: 16 additions & 0 deletions LeetCode/Problems/Python/ZigZag Conversion/ZigZag Conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows < 2:
return s
i = 0
res = [""]*numRows # We will fill in each line in the zigzag
for letter in s:
if i == numRows-1: # If this is the last line in the zigzag we go up
grow = False
elif i == 0: #Otherwise we go down
grow = True
res[i] += letter #Add the letter to its row
i = (i+1) if grow else i-1 # We increment (add 1) if grow is True,
# and decrement otherwise

return "".join(res) # return the joined rows