看板 Marginalman
2326. Spiral Matrix IV ## 思路 先建default=-1的matrix 跑linked list 每次檢查下一個(nr,nc) 能不能填, 不能填就換方向 ## Code ```python class Solution: def spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]: res = [[-1] * n for _ in range(m)] res[0][0] = head.val head = head.next r = c = i = 0 dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] while head: nr, nc = r + dirs[i][0], c + dirs[i][1] if 0 <= nr < m and 0 <= nc < n and res[nr][nc] == -1: res[nr][nc] = head.val head = head.next r, c = nr, nc else: i = (i+1) % 4 return res ``` -- https://i.imgur.com/kyBhy6o.jpeg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.13 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725853515.A.07C.html