题解 | #顺时针打印矩阵#
顺时针打印矩阵
https://www.nowcoder.com/practice/9b4c81a02cd34f76be2659fa0d54342a
# -*- coding:utf-8 -*- class Solution: # matrix类型为二维列表,需要返回列表 def printMatrix(self, matrix): # write code here res=[] def inner(a,res): # 将打印半圈视为一次操作 R,C=len(a),len(a[0]) for i in range(R): if i==0: for _ in range(C): v=a[0].pop(0) res.append(v) else: res.append(a[i].pop()) a.remove([]) while matrix and matrix[0]: inner(matrix,res) #打印半圈后,反转剩余矩阵,再执行打印半圈操作 for i in range(len(matrix)): matrix[i].reverse() matrix.reverse() return res