54. Spiral Matrix
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Math & Geometry
- Time
- O(M*N)
- Space
- O(1)
Standard Solution (C++)
SummaryGiven an m x n integer matrix, return all elements collected in spiral order starting from the top-left corner and moving clockwise inward.
Approach
- Maintain four boundaries (top, bottom, left, right) that shrink inward after each directional pass.
- Traverse: left-to-right along the top row, top-to-bottom along the right column, right-to-left along the bottom row, and bottom-to-top along the left column — then tighten all four bounds.
- A guard check before each reverse pass (bottom row and left column) prevents double-counting when a single row or column remains.
- Continue until the boundaries cross, naturally handling both square and non-square matrices.
#include <vector>
using namespace std;
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> result;
int top = 0, bottom = (int)matrix.size() - 1;
int left = 0, right = (int)matrix[0].size() - 1;
while (top <= bottom && left <= right) {
// traverse top row left → right
for (int j = left; j <= right; ++j)
result.push_back(matrix[top][j]);
++top;
// traverse right column top → bottom
for (int i = top; i <= bottom; ++i)
result.push_back(matrix[i][right]);
--right;
// traverse bottom row right → left (only if a row remains)
if (top <= bottom) {
for (int j = right; j >= left; --j)
result.push_back(matrix[bottom][j]);
--bottom;
}
// traverse left column bottom → top (only if a column remains)
if (left <= right) {
for (int i = bottom; i >= top; --i)
result.push_back(matrix[i][left]);
++left;
}
}
return result;
}
};
Time O(m * n) Space O(1)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> res;
int m=matrix.size(); // 3
int n=matrix[0].size(); // 4
// loop 0: (0, 0) -> (0, n-1) -> (m-1, n-1) -> (m-1, 0) -> (1, 0)
// loop 1: (1, 1) -> (1, n-2) -> (m-2, n-2) -> (m-2, 1) -> (2, 1)
// loop 2: (2, 2) -> ...
int totLoop=std::min(m, n)/2;
int curLoop=0;
while(curLoop<totLoop){
// first row
for(int j=curLoop; j<=n-1-curLoop; ++j){
res.push_back(matrix[curLoop][j]);
}
// last col
for(int i=curLoop+1; i<=m-1-curLoop; ++i){
res.push_back(matrix[i][n-1-curLoop]);
}
// last row
for(int j=n-2-curLoop; j>=curLoop; --j){
res.push_back(matrix[m-1-curLoop][j]);
}
// first col
for(int i=m-2-curLoop; i>curLoop; --i){
res.push_back(matrix[i][curLoop]);
}
++curLoop;
}
if(std::min(m, n)%2==1){
if(m<=n){
for(int j=curLoop; j<=n-1-curLoop; ++j){
res.push_back(matrix[curLoop][j]);
}
}
else{
for(int i=curLoop; i<=m-1-curLoop; ++i){
res.push_back(matrix[i][curLoop]);
}
}
}
return res;
}
};
// Time Complexity: O(M*N)
// Space Complexity: O(1) Time O(M*N) Space O(1)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.