Skip to content

Commit 3395f2a

Browse files
authored
Add files via upload
1 parent 7c6e40f commit 3395f2a

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

剑指Offer/19.顺时针打印矩阵.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,26 @@ def printMatrix(self, matrix):
2222
for i in range(endx-1, start, -1): # 最左列
2323
result.append(matrix[i][start])
2424
start += 1
25-
return result
25+
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+
if matrix and matrix[0]:
45+
for row in matrix[::-1]:
46+
res.append(row.pop(0))
47+
return res

0 commit comments

Comments
 (0)