AcWing 40. 顺时针打印矩阵-python -巧解
原题链接
中等
作者:
acwing_827
,
2019-09-04 16:32:13
,
所有人可见
,
阅读 1191
class Solution(object):
def printMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[int]
"""
# 方法一:通过旋转矩阵不断输出第一行
if matrix:
top_row = list(matrix[0])
array = list(zip(*matrix[1:]))[::-1]
return top_row + self.printMatrix(array)
return []
#方法二:顺时针对矩阵 pop()
res = []
while matrix:
res += matrix.pop(0)
if matrix and matrix[0]:
for row in matrix:
res.append(row.pop())
if matrix:
res += matrix.pop()[::-1]
if matrix and matrix[0]:
for row in matrix[::-1]:
res.append(row.pop(0))
return res