0%

6. Zigzag Conversion

https://leetcode.com/problems/zigzag-conversion/description/

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
class Solution:
def convert(self, s: str, nrows: int) -> str:
if nrows==1:
return s
D=defaultdict(list)
# traverse row and once reach the ceil or bottom, then
# change direction and increase/stop increase column
k=0
i=0
j=0
direction=True
increase=False
while k<len(s):
D[i].append(s[k])
k+=1
if direction:
i+=1
else:
i-=1
if increase:
j+=1
if k%(nrows-1)==0:
direction=not direction
increase=not increase
R=""
for row in range(nrows):
R+="".join(D[row])
return R

Actually, we don’t need increase variable. But it is simulation, so i keep it there.