-
Notifications
You must be signed in to change notification settings - Fork 612
/
06.py
41 lines (33 loc) · 923 Bytes
/
06.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'''
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
'''
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1:
return s
result = ["" for _ in range(numRows)]
row, down = 0, 1
for char in s:
result[row] += char
if row == numRows - 1:
down = 0
if row == 0:
down = 1
if down:
row += 1
else:
row -= 1
final_string = ""
for value in result:
final_string += value
return final_string
print Solution().convert("PAYPALISHIRING",3)