『剣指offer』の時計回り印刷マトリクス(python実装)
問題の説明:
1つのマトリクスを入力、各数字を時計回りに順次印刷し、例えば、1 2 3 4 4 4 5 6 6 7 8 8 11 12 13 14 16を入力と、1,2,3,4,8,12,16,15,14,13,9,6,7,11,10の順に印刷する.
pythonコードは次のように実装されます.
プログラムのzip関数の役割:
1つのマトリクスを入力、各数字を時計回りに順次印刷し、例えば、1 2 3 4 4 4 5 6 6 7 8 8 11 12 13 14 16を入力と、1,2,3,4,8,12,16,15,14,13,9,6,7,11,10の順に印刷する.
:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 2 3 4, :
5 6 7 8
9 10 11 12
13 14 15 16
8 12 16
7 11 15
6 10 14
5 9 13
8 12 16
pythonコードは次のように実装されます.
# -*- coding:utf-8 -*-
class Solution:
# matrix ,
def printMatrix(self, matrix):
# write code here
l=[]
while matrix:
l += matrix[0]
del matrix[0]
matrix=zip(*matrix)[::-1]
return l
プログラムのzip関数の役割:
zip() , , 。
, , * , 。
zip Python 2 Python 3 : Python 3.x ,zip() 。 , list() 。
zip([iterable, ...])
:
iterabl -- ;
。