Skip to content

Commit 0a06253

Browse files
committed
add case
1 parent 51d03da commit 0a06253

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

Medium/zigzag_conversion.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,30 @@ def convert(self, s, numRows):
55
:type numRows: int
66
:rtype: str
77
"""
8-
mapLetter = [[] for _ in range(numRows)]
9-
# strReverse = list(s)[::-1]
10-
# print(strReverse)
11-
start = 0
12-
iLetter = 0
8+
if len(s) == 0 or len(s) > 1000 or numRows <= 1 or numRows > 1000:
9+
return s
1310

14-
for i, _ in enumerate(mapLetter):
15-
if iLetter > len(s):
16-
break
17-
18-
if start == 0:
19-
mapLetter[i].append(s[iLetter])
20-
21-
iLetter += 1
11+
collectZigzag = ["" for _ in range(numRows)]
12+
currRow = 0
13+
isStraight = True
14+
15+
for _, c in enumerate(s):
16+
if currRow == 0:
17+
isStraight = True
18+
elif currRow == numRows - 1:
19+
isStraight = False
2220

23-
if start < numRows:
24-
start = 0
21+
collectZigzag[currRow] += c
22+
23+
if isStraight:
24+
currRow += 1
2525
else:
26-
start += 1
27-
28-
print(mapLetter)
26+
currRow -= 1
27+
28+
29+
return "".join(collectZigzag)
2930

3031

31-
print(Solution().convert("PAYPALISHIRING", 3))
32+
# print(Solution().convert("PAYPALISHIRING", 3))
33+
print(Solution().convert("AB", 1))
3234

0 commit comments

Comments
 (0)