-
Notifications
You must be signed in to change notification settings - Fork 0
/
6. ZigZag Conversion
35 lines (35 loc) · 1.06 KB
/
6. ZigZag Conversion
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
# 12:30 -- 13:31:
# run time: 102ms, 76%; 112ms, 64%; 95ms, 87%
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
length = len(s)
if numRows <= 1 or length <= numRows:
return s
numset = 2 * numRows -2
temp = 0
output = ''
while temp <= numRows - 1:
if temp == 0 or temp == numRows- 1:
start = temp
while start < len(s):
output += s[start]
start += numset
else:
start = temp
end = numset - start
while start < ( length - 1) / numset * numset:
output += s[start]
output += s[end]
start += numset
end += numset
if length > start:
output += s[start]
if length > end:
output += s[end]
temp += 1
return output