题解 | #螺旋矩阵#
螺旋矩阵
https://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31
from os import name # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param matrix int整型二维数组 # @return int整型一维数组 # from typing import List, Literal class Solution: def spiralOrder(self , matrix: List[List[int]]) -> List[int]: # write code here if len(matrix) == 0: return [] n, m = len(matrix), len(matrix[0]) dirs4 = [(-1, 0), (0, 1), (1, 0), (0, -1)] ans = [] visited = [[False] * m for _ in range(n)] x, y, d = 0, 0, 1 Num = n * m for i in range(Num): ans.append(matrix[x][y]) visited[x][y] = True a, b = x + dirs4[d][0], y + dirs4[d][1] if a < 0 or a >= n or b < 0 or b >= m or visited[a][b]: d = (d + 1) % 4 a = x + dirs4[d][0] b = y + dirs4[d][1] x = a y = b return ans
算法刷题记录 文章被收录于专栏
刷题,记录牛客的101