leetcode 每日一题 54. 螺旋矩阵
模拟
思路:
遍历元素,每当路径要超出界限或者进入之前访问的单元格时,会顺时针旋转方向。
代码:
class Solution(object): def spiralOrder(self, matrix): if not matrix: return [] R, C = len(matrix), len(matrix[0]) seen = [[False] * C for _ in matrix] ans = [] dr = [0, 1, 0, -1] dc = [1, 0, -1, 0] r = c = di = 0 for _ in range(R * C): ans.append(matrix[r][c]) seen[r][c] = True cr, cc = r + dr[di], c + dc[di] if 0 <= cr < R and 0 <= cc < C and not seen[cr][cc]: r, c = cr, cc else: di = (di + 1) % 4 r, c = r + dr[di], c + dc[di] return ans
按层模拟
思路:
一层一层遍历。
代码:
class Solution(object): def spiralOrder(self, matrix): def spiral_coords(r1, c1, r2, c2): for c in range(c1, c2 + 1): yield r1, c for r in range(r1 + 1, r2 + 1): yield r, c2 if r1 < r2 and c1 < c2: for c in range(c2 - 1, c1, -1): yield r2, c for r in range(r2, r1, -1): yield r, c1 if not matrix: return [] ans = [] r1, r2 = 0, len(matrix) - 1 c1, c2 = 0, len(matrix[0]) - 1 while r1 <= r2 and c1 <= c2: for r, c in spiral_coords(r1, c1, r2, c2): ans.append(matrix[r][c]) r1 += 1; r2 -= 1 c1 += 1; c2 -= 1 return ans