-- coding:utf-8 --

class Solution:
# matrix类型为二维列表,需要返回列表
output=[]
def printMatrix(self, matrix):
# write code here
return self.print_matrix(matrix,self.output)
def print_matrix(self,matrix,output):
n=len(matrix)#行数
if n!=0:
m=len(matrix[0])#列数
else:
m=0
if m==1 and n!=1:
return self.output+[matrix[i][0] for i in range(n)]
if n==0 or m==0:
return self.output
if n==1:
return self.output+matrix[0]
#输出外层
top=matrix[0]
bottom=matrix[-1][-2::-1]
right=[matrix[i+1][-1] for i in range(n-1)]
left=[matrix[n-2-i][0] for i in range(n-2)]
self.output=self.output+top+right+bottom+left
matrix_core=[]
for i in range(n-2):
#取中间的矩阵块
matrix_core.append(matrix[i+1][1:-1])
#递归
return self.print_matrix(matrix_core,output)