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 7c6e40f commit 3395f2aCopy full SHA for 3395f2a
剑指Offer/19.顺时针打印矩阵.py
@@ -22,4 +22,26 @@ def printMatrix(self, matrix):
22
for i in range(endx-1, start, -1): # 最左列
23
result.append(matrix[i][start])
24
start += 1
25
- return result
+ return result
26
+
27
28
+class Solution:
29
+ def printMatrix(self, matrix):
30
+ if not matrix:
31
+ return []
32
+ res = []
33
+ while matrix:
34
+ # 弹出第一行
35
+ res += matrix.pop(0)
36
+ # 添加最后一列。非空的行才能弹出元素,故需先判断是否为空
37
+ if matrix and matrix[0]:
38
+ for row in matrix:
39
+ res.append(row.pop())
40
+ # 弹出最后一行并反转
41
+ if matrix:
42
+ res += matrix.pop()[::-1]
43
+ # 添加第一列
44
45
+ for row in matrix[::-1]:
46
+ res.append(row.pop(0))
47
+ return res
0 commit comments