Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ZigZag Conversion #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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