Skip to content

Instantly share code, notes, and snippets.

@DataSolveProblems
Created September 17, 2019 23:39
Show Gist options
  • Save DataSolveProblems/6c410d33d83473a57cb218d2c74cc838 to your computer and use it in GitHub Desktop.
Save DataSolveProblems/6c410d33d83473a57cb218d2c74cc838 to your computer and use it in GitHub Desktop.
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1:
return s
zigzag = [[] for _ in range(numRows)]
row = 0
direction = -1
for c in s:
zigzag[row].append(c)
if row == 0 or row == numRows - 1:
direction = -direction
row += direction
return ''.join([c for r in zigzag for c in r])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment