-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag_conversion.py
More file actions
44 lines (34 loc) · 1.08 KB
/
zigzag_conversion.py
File metadata and controls
44 lines (34 loc) · 1.08 KB
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
42
43
44
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1:
return s
s_len = len(s)
ret_s = str()
for row in range(0,numRows):
cycle = 0
while True:
if row == 0 or row == numRows-1:
index = row + 2*(numRows-1)*cycle
cycle += 1
if (index < s_len):
ret_s += s[index]
else:
break
else:
index1 = row + 2*(numRows-1)*cycle
index2 = index1 + 2*(numRows-row-1)
cycle += 1
if (index1 < s_len):
ret_s += s[index1]
else:
break
if (index2 < s_len):
ret_s += s[index2]
else:
break
return ret_s