-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0006_zigzag_conversion.py
More file actions
64 lines (55 loc) · 2.08 KB
/
0006_zigzag_conversion.py
File metadata and controls
64 lines (55 loc) · 2.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution:
def convert(self, s: str, numRows: int) -> str:
'''
Ex) "PAYPALISHIRING", 4
>>> "PIN ALSIG YAH RPI"
PAYP A L ISHI R I NG
len = 14
PIN ALSIG YAHR PI
0 6 12, 1 5 7 11 13, 2 4 8 10, 3 9
Number of Col = len(s) div (numRows + numRows - 2)
= 14 div (4 + 4 - 2)
= 14 div 6
= 2
if len(s) mod (numRows + numRows - 2) > numRows - 2
then Number of Col += 1
P I N || 0 6 12
A L S I G || 1 5 7 11 13
Y A H R || 2 4 8 10
P I || 3 9
'''
zigzag = ""
# Dictionary of number of characters in each row
numRowDic = {}
for i in range(numRows):
# Count number of characters in each row
mod = 2 * numRows - 2
# First Row
if (i == 0):
colNum = len(s) // mod
if (len(s) % mod >= numRows - 2):
colNum += 1
# Use mod to get the ith row
for j in range(colNum):
zigzag += s[j * mod]
# Last Row
elif (i == numRows - 1):
colNum = 2 * numRowDic[0]
if (len(s) % mod < i + 1):
colNum -= 1
# Use mod to get the ith row
for j in range(colNum):
zigzag += s[j * mod + i]
# Middle Row
else:
colNum = 2 * numRowDic[0] - 1
if (len(s) % mod > i + 1):
colNum += mod // i
# Use mod to get the ith row
for j in range(colNum // 2):
zigzag += s[j * mod + i]
if (j < colNum - 1 or colNum % 2 == 0):
zigzag += s[mod - i]
# Add colNum in dictionary
numRowDic[i] = colNum
return zigzag