剑指offer-19-顺时针打印矩阵
顺时针打印矩阵
http://www.nowcoder.com/questionTerminal/9b4c81a02cd34f76be2659fa0d54342a
# -*- coding:utf-8 -*-
class Solution:
# matrix类型为二维列表,需要返回列表
def printMatrix(self, matrix):
# write code here
if matrix==None:
return None
##找到四个边
low=0
height=len(matrix)-1
left=0
right=len(matrix[0])-1
result=[]
while low<=height and left<=right:
for i in range(left,right+1):
result.append(matrix[low][i])
for j in range(low+1,height+1):
result.append(matrix[j][right])
if low<height: ##防止重复操作
for k in range(right-1,left-1,-1):
result.append(matrix[height][k])
if left<right: ##防止重复操作
for l in range(height-1,low,-1):
result.append(matrix[l][left])
##缩小矩阵
low+=1
height-=1
left+=1
right-=1
return result