mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 146 ms (35.98%), Space: 13.4 MB (93.22%) - LeetHub
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
class Solution(object):
|
||||
def convert(self, s, numRows):
|
||||
"""
|
||||
:type s: str
|
||||
:type numRows: int
|
||||
:rtype: str
|
||||
"""
|
||||
|
||||
# Base case: numRows = 1
|
||||
if numRows == 1:
|
||||
return s
|
||||
|
||||
output = ""
|
||||
interval = (2 * numRows) - 2
|
||||
for row in range(numRows):
|
||||
letterIndex = row
|
||||
diagonalIndex = (2 * numRows) - row - 2
|
||||
while letterIndex < len(s):
|
||||
# Gets letters in vertical lines
|
||||
output += s[letterIndex]
|
||||
letterIndex += interval
|
||||
|
||||
# Top and bottom rows have no diagonal pieces
|
||||
if row == 0 or row == numRows - 1:
|
||||
continue
|
||||
|
||||
# Catch end of string
|
||||
if diagonalIndex >= len(s):
|
||||
continue # Maybe break should work
|
||||
|
||||
# Gets letters on diagonals
|
||||
output += s[diagonalIndex]
|
||||
diagonalIndex += interval
|
||||
|
||||
return output
|
||||
|
||||
Reference in New Issue
Block a user