-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0006_zigzag_conversion.py
More file actions
54 lines (47 loc) · 1.13 KB
/
0006_zigzag_conversion.py
File metadata and controls
54 lines (47 loc) · 1.13 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
45
46
47
48
49
50
51
52
53
54
import time
start_time = time.time()
# s = "PAYPALISHIRING"
s = "A"
numRows = 3
numRows = 4
numRows = 1
# P I N
# A L S I G
# Y A H R
# P I
# 0 6 12
# 1 5 7 11 13
# 2 4 8 10
# 3 9
class Solution:
def convert(self, s: str, numRows: int) -> str:
r = ""
l = len(s)
c = numRows
w = l//c+1 # count waves
if w<=0:
w=1
d = c-2 # count chars in diagonale
if d<=0:
d=0
print("w=",w," , ","d=",d)
for n in range(c):
print(n,"--")
for j in range(w):
print(j)
if n+(c+d)*j < l:
ii = n+(c+d)*j
print(ii , s[ii])
r += s[n+(c+d)*j]
if n in range(1,c-1) and (c+d-n+(c+d)*j < l):
ii = c+d-n+(c+d)*j
print(ii , s[ii], s)
r+=s[c+d-n+(c+d)*j]
# print(r)
# if r=="":
return r
q = Solution()
print(q.convert(s=s, numRows=numRows))
print("")
print("---")
print(time.time() - start_time, "seconds")