Skip to content

Commit d00c252

Browse files
committed
feat: zigzag-conversion
1 parent cc38add commit d00c252

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

arr.zigzag-conversion.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
"""
3+
6. Z 字形变换
4+
https://leetcode-cn.com/problems/zigzag-conversion/
5+
将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排
6+
"""
7+
def convert(self, s: str, numRows: int) -> str:
8+
l = len(s)
9+
if numRows <= 1:
10+
return s
11+
12+
res = ['' for _ in range(numRows)]
13+
p = 2 * numRows - 2
14+
for i in range(l):
15+
remainder = i % p
16+
if remainder < numRows:
17+
res[remainder] += s[i]
18+
else:
19+
res[p - remainder] += s[i]
20+
return ''.join(res)
21+
22+
23+
so = Solution()
24+
# LCIRETOESIIGEDHN
25+
print(so.convert('LEETCODEISHIRING', 3))
26+
# LDREOEIIECIHNTSG
27+
print(so.convert('LEETCODEISHIRING', 4))
28+
print(so.convert('A', 1))

0 commit comments

Comments
 (0)