算法参考:
剑指offer面试题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字 - CSDN博客 http://blog.csdn.net/yanxiaolx/article/details/52254590
剑指offer_输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字 - CSDN博客 http://blog.csdn.net/lingongheng/article/details/52725192
python代码:
方法一:
class Solution:
# matrix类型为二维列表,需要返回列表
#剑指offer面试题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字 - CSDN博客 http://blog.csdn.net/yanxiaolx/article/details/52254590
def printMatrix(self, matrix):
# write code here
if matrix==[[]]:
return
row=len(matrix)
column=len(matrix[0])
left=0
right=column-1
top=0
boom=row-1
res=[]
while right>left and toptop:
for i in range(top,boom+1):
res.append(matrix[i][left])
#剩下一个
if boom==top and left==right:
res.append(matrix[left][top])
return res
# -*- coding:utf-8 -*-
class Solution:
# matrix类型为二维列表,需要返回列表
def printMatrix(self, matrix):
# write code here
if matrix==[[]]:
return
row=len(matrix)
column=len(matrix[0])
start=0
res=[]
while row>start*2 and column>start*2:
endX=column-1-start
endY=row-1-start
#从左到右打印一行
for i in range(start,endX+1):
res.append(matrix[start][i])
#print(matrix[start][i])
#从上往下打印一列
if start