1. 程式人生 > >順時針列印矩陣並輸出

順時針列印矩陣並輸出

import cProfile
def printMatrix(matrix):
    result=[]
    while matrix:
        result.append(matrix.pop(0))
        if not matrix or not matrix[0]:
            break
        else:
            matrix=[[r[col] for r in matrix] for col in range(len(matrix[0])-1,-1,-1)]
            print(matrix)
    result1 =[n for a in result for n in a]   
    return result1
a=[[1,2,3],[4,5,6],[7,8,9]]
print(printMatrix(a))
cProfile.run("printMatrix")

思想:玩魔方,每次刪去矩陣第一行