class Printer: def clockwisePrint(self, mat, n, m): # write code here # 对角线表开始。奇表示一行或者一列结尾 l = [] diag_n = int(n / 2) diag_m = int(m / 2) for i in range(0, min(diag_m, diag_n)): for j in range(i, m-i): l.append(mat[i][j]) for i_ in range(i+1, n-i): l.append(mat[i_][m-i-1]) for j_ in range(m-i-2, i-1, -1): l.append(mat[n-i-1][j_]) for i_ in range(n-i-2, i, -1): l.append(mat[i_][i]) flag_n = n % 2 != 0 flag_m = m % 2 != 0 if diag_n > diag_m and flag_m: for i in range(min(diag_m, diag_n),min(diag_m, diag_n)+(n-2*int((m-1)/2))): l.append(mat[i][min(diag_m, diag_n)]) elif diag_n < diag_m and flag_n: for j in range(min(diag_m, diag_n), min(diag_m, diag_n) + (m - 2*int((n - 1) / 2))): l.append(mat[min(diag_m, diag_n)][j]) elif diag_n == diag_m and flag_n and flag_m: l.append(mat[diag_n][diag_n]) return l
# -*- coding:utf-8 -*-
class Printer:
def clockwisePrint(self, mat, n, m):
ret = []
while len(mat) > 0:
ret += mat[0] #将矩阵的首行放入 ret
mat = [x for x in zip(*mat[1:])][::-1] #去掉矩阵首行后逆时针旋转90度
return ret
class Printer:
def clockwisePrint(self, mat, n, m):
res = []
while mat:
res += mat.pop(0)
if mat and mat[0]:
for row in mat:
res.append(row.pop())
if mat:
res += mat.pop()[::-1]
if mat and mat[0]:
for row in mat[::-1]:
res.append(row.pop(0))
return res