54-Spiral-Matrix
Sat 17 May 2025
https://leetcode.com/problems/spiral-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix:
return []
m = len(matrix)
n = len(matrix[0])
ans = []
r1 = 0
c1 = 0
r2 = m - 1
c2 = n - 1
# Repeatedly add matrix[r1..r2][c1..c2] to ans
while len(ans) < m * n:
j = c1
while j <= c2 and len(ans) < m * n:
ans.append(matrix[r1][j])
j += 1
i = r1 + 1
while i <= r2 - 1 and len(ans) < m * n:
ans.append(matrix[i][c2])
i += 1
j = c2
while j >= c1 and len(ans) < m * n:
ans.append(matrix[r2][j])
j -= 1
i = r2 - 1
while i >= r1 + 1 and len(ans) < m * n:
ans.append(matrix[i][c1])
i -= 1
r1 += 1
c1 += 1
r2 -= 1
c2 -= 1
return ans
new Solution().spiralOrder()
Score: 5
Category: leetcode