220. Spiral Matrix
Medium
Boundary Simulation
Array
Problem
Given an m x n matrix, return all elements of the matrix in spiral order.
Examples
Example 1
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
Example 2
Input: [[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]
Example 3
Input: [[1]]
Output: [1]
Constraints
� 1 <= matrix.length <= 200
� 1 <= matrix[i].length <= 200
Hints
?? Track top, bottom, left and right boundaries.
?? Shrink the appropriate boundary after each directional traversal.
Expected Complexity
Time: O(m * n)
Space: O(1) auxiliary
Follow-up
Can you implement it without a visited matrix?
Practice Notes
Attempts: 0
Time spent: 0min
Hints used: 0
JavaScript
00:00
Loading...
Case 1
Case 2
Case 3
Input
[[1,2,3],[4,5,6],[7,8,9]]Expected
[1,2,3,6,9,8,7,4,5]