如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# -*- coding:utf-8 -*-
class Solution:
# matrix类型为二维列表,需要返回列表
def printMatrix( self , matrix):
# write code here
res = []
n = len (matrix)
m = len (matrix[ 0 ])
if m = = 1 and n = = 1 :
res = [matrix[ 0 ][ 0 ]]
return res
else :
for o in range (( min (m,n) + 1 ) / / 2 ):
[res.append(matrix[o][i]) for i in range (o,m - o)]
[res.append(matrix[j][m - o - 1 ]) for j in range (o,n - o) if matrix[j][m - o - 1 ] not in res]
[res.append(matrix[n - o - 1 ][k]) for k in range (m - 1 - o,o - 1 , - 1 ) if matrix[n - o - 1 ][k] not in res]
[res.append(matrix[l][o]) for l in range (n - 1 - o,o - 1 , - 1 ) if matrix[l][o] not in res]
return res
|
以上这篇python 顺时针打印矩阵的超简洁代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/nillei/article/details/60962831