We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cc38add commit d00c252Copy full SHA for d00c252
arr.zigzag-conversion.py
@@ -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