classSolution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: iflen(matrix)== 0: return [] a = [] M = len(matrix) N = len(matrix[0]) levels = (N+1)//2 if M > N else (M+1)//2; for i in range(levels): for j in range(i,N - i): a.append(matrix[i][j]) for j in range(i + 1,M - i): a.append(matrix[j][N - i - 1]) for j in range(N-i-2,i-1,-1): if M - i -1 == i: break a.append(matrix[M - i - 1][j]) for j in range(M-i-2,i,-1): if N - i -1 == i: break a.append(matrix[j][i]) return a;
classSolution: defspiralOrder(self, matrix: List[List[int]]) -> List[int]: if len(matrix) == 0: return [] a = [] M = len(matrix) N = len(matrix[0]) levels = (N+1)//2if M > N else (M+1)//2; for i in range(levels): for j in range(i,N - i): a.append(matrix[i][j]) for j in range(i + 1,M - i): a.append(matrix[j][N - i - 1]) for j in range(N-i-2,i-1,-1): if M - i -1 == i: break a.append(matrix[M - i - 1][j]) for j in range(M-i-2,i,-1): if N - i -1 == i: break a.append(matrix[j][i]) return a;