码迷,mamicode.com
首页 > 编程语言 > 详细

剑指offer python版 顺时针打印矩阵

时间:2018-10-26 10:47:01      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:pytho   for   end   bsp   打印矩阵   offer   turn   append   print   

def aa(matrix):
    rows=len(matrix)
    cols=len(matrix[0])
    start=0
    ret=[]
    while start*2 <rows and start*2<cols:
        bb(matrix,rows,cols,start,ret)
        start +=1
        
    return ret


def bb(matrix,rows,cols,start,ret):
    row=rows-start-1
    col=cols-start-1
    
    for c in range(start,col+1):
        ret.append(matrix[start][c])
        
    if start <row:
        for r in range(start+1,row+1):
            ret.append(matrix[r][col])
            
    if start <row and start<col:
        for c in range(start,col)[::-1]:
            ret.append(matrix[row][c])
            
    if start <row and start<col:
        for r in range(start+1,row)[::-1]:
            ret.append(matrix[r][start])
            
a=[[1,2,3],
   [4,5,6],
   [7,8,9]]

print(aa(a))

 

剑指offer python版 顺时针打印矩阵

标签:pytho   for   end   bsp   打印矩阵   offer   turn   append   print   

原文地址:https://www.cnblogs.com/xzm123/p/9854659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!