题目描述

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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

最新文章

  1. Matter.js – 你不能错过的 2D 物理引擎
  2. hadoop2.5重新编译问题
  3. C++vector迭代器失效的问题
  4. Java-基础编程(螺旋矩阵&amp;乘法表)
  5. Mybatis 级联查询 (一对多 )
  6. erl0008 - unicode 和 utf-8之间的关系
  7. Revit二次开发-BIM模型导出
  8. Adobe Dreamweaver CS6 序列号 注册码(转自91zcm)
  9. 通过Mouse Without Borders在多台机器上共享键盘鼠标
  10. library cahce pin
  11. 构建混合云:配置Azure site to site VPN连接(2)
  12. 2014.3.6-C语言学习小结
  13. 01_change_schema.sql
  14. HTTP协议分析
  15. Android studio打开项目一直卡住
  16. Android快速关联V4包的方式
  17. WPF 引用 ttf文件
  18. [学习笔记]Java作用域变量申明和C、C++的差异性
  19. @1-2初识Python爬虫
  20. 【转】memcached分布式部署

热门文章

  1. Python 多客户端
  2. 在mac下运行 npm run eject 出现报错问题解决方法
  3. http请求常用的状态码
  4. React最佳实践(1)
  5. 『C++』Temp_2018_12_06
  6. 集合Gk表示这样一堆数字,该集合内的数字有k个1
  7. SpringBoot整合Mybatis,TypeAliases配置失败的问题
  8. 创建jq插件步骤
  9. 关于css属性calc对于ie的态度
  10. Java三种代理模式