54. Spiral Matrix
Problem
Given an m x n matrix, return all elements of the matrix in spiral order.
https://leetcode.com/problems/spiral-matrix/
Example 1:

Input:
matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output:[1,2,3,6,9,8,7,4,5]
Example 2:

Input:
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output:[1,2,3,4,8,12,11,10,9,5,6,7]
Constraints:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 10-100 <= matrix[i][j] <= 100
Test Cases
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:solution_test.py下载完整源码
import pytest
from solution import Solution
@pytest.mark.parametrize('matrix, expected', [
([[1,2,3],[4,5,6],[7,8,9]], [1,2,3,6,9,8,7,4,5]),
([[1,2,3,4],[5,6,7,8],[9,10,11,12]], [1,2,3,4,8,12,11,10,9,5,6,7]),
])
class Test:
def test_solution(self, matrix, expected):
sol = Solution()
assert sol.spiralOrder(matrix) == expected
Thoughts
主要就是注意控制好边界。
Code
solution.py下载完整源码
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
m = len(matrix)
n = len(matrix[0])
res = [0] * (m * n)
steps = ((0, 1), (1, 0), (0, -1), (-1, 0)) # →, ↓, ←, ↑
limits = [n, m - 1] # ↔︎, ↕
i = 0
j = -1
dir = 0
move = 0
for idx in range(m * n):
i += steps[dir][0]
j += steps[dir][1]
res[idx] = matrix[i][j]
move += 1
if move == limits[dir & 1]:
limits[dir & 1] -= 1
dir = (dir + 1) % 4
move = 0
return res
评论需要 JavaScript。