剑指offer-顺时针打印矩阵19
2024-10-21 09:36:12
题目描述
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
class Solution:
# matrix类型为二维列表,需要返回列表
def printMatrix(self, matrix):
row=len(matrix)
if row==0:
return None
#初始化相关参数
col=len(matrix[0])
reslist=[]
startRow=0
endRow=row-1
startCol=0
endCol=col-1
while startRow<=endRow and startCol<=endCol:
#如果就剩下一行
if startRow==endRow:
i=startCol
while i<=endCol:
reslist.append(matrix[startRow][i])
i+=1
return reslist
#如果就剩下一列
if startCol==endCol:
i=startCol
while i<=endRow:
reslist.append(matrix[i][startCol])
i+=1
return reslist
#首行
i=startCol
while i<=endCol:
reslist.append(matrix[startRow][i])
i+=1
#末列
i=startRow+1
while i<=endRow:
reslist.append(matrix[i][endCol])
i+=1
#末行
i=endCol-1
while i>=startCol:
reslist.append(matrix[endRow][i])
i-=1
#首列
i=endRow-1
while i>=startRow+1:
reslist.append(matrix[i][startCol])
i-=1
startRow+=1
startCol+=1
endRow-=1
endCol-=1
return reslist
最新文章
- Matter.js – 你不能错过的 2D 物理引擎
- hadoop2.5重新编译问题
- C++vector迭代器失效的问题
- Java-基础编程(螺旋矩阵&;乘法表)
- Mybatis 级联查询 (一对多 )
- erl0008 - unicode 和 utf-8之间的关系
- Revit二次开发-BIM模型导出
- Adobe Dreamweaver CS6 序列号 注册码(转自91zcm)
- 通过Mouse Without Borders在多台机器上共享键盘鼠标
- library cahce pin
- 构建混合云:配置Azure site to site VPN连接(2)
- 2014.3.6-C语言学习小结
- 01_change_schema.sql
- HTTP协议分析
- Android studio打开项目一直卡住
- Android快速关联V4包的方式
- WPF 引用 ttf文件
- [学习笔记]Java作用域变量申明和C、C++的差异性
- @1-2初识Python爬虫
- 【转】memcached分布式部署