-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_zigzag.py
39 lines (37 loc) · 1.13 KB
/
6_zigzag.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
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
rows = []
for x in range(0,numRows):
rows.append([])
count = 0
sign = True
for i in range(0, len(s)):
rows[count].append(s[i])
if numRows == 1:
continue
if sign:
if count == numRows - 1:
sign = not sign
count = count - 1
else:
count = count + 1
else:
if count == 0:
sign = not sign
count = count + 1
else:
count = count - 1
# for j in range(0,numRows):
# print rows[j]
# print '\n'
return ''.join([''.join(row) for row in rows])
if __name__ == '__main__':
print Solution().convert('PAYPALISHIRING',3) #PAHNAPLSIIGYIR
print Solution().convert('ABCD',2) #ACBD
print Solution().convert('ABC',2) #ACB
print Solution().convert('ABC',1) #ACB