Medium
Spiral Matrix
Medium
0 submissions
25 coins
+100 XP
Array
Matrix
Problem Description
Given an `m x n` `matrix`, return all elements of the `matrix` in spiral order.
**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]
**Example 3:**
Input: matrix = [[1]]
Output: [1]
**Hint:** Use four boundary pointers (top, bottom, left, right) and shrink them as you traverse each layer of the spiral.
Constraints
- m == matrix.length
- n == matrix[i].length
- 1 <= m, n <= 10
- -100 <= matrix[i][j] <= 100
Need help?
Connect with expert programmers for real-time collaborative coding, video meetings, and whiteboard sessions via CodeConnect.
Video Call
Whiteboard
Live Coding
Screen Share