The "Spiral Matrix" problem requires you to traverse a 2D matrix in a spiral order, starting from the top-left corner and moving in a clockwise direction. The task is to return all the elements of the matrix in this spiral order.
To solve this problem, imagine "peeling" the matrix layer by layer, starting from the outermost layer and moving inward. We start by traversing the top row, then the right column, then the bottom row, and finally the left column. After completing one full cycle, we move inward and repeat the process until we have processed all elements of the matrix.
-
Initialize Boundaries:
- Set four boundaries:
top
,bottom
,left
, andright
to define the current layer of the matrix that we are traversing. - Initially,
top
is 0,bottom
is the last row,left
is 0, andright
is the last column.
- Set four boundaries:
-
Traverse in Spiral Order:
- Left to Right: Traverse the
top
row from theleft
boundary to theright
boundary, then move thetop
boundary downwards. - Top to Bottom: Traverse the
right
column from thetop
boundary to thebottom
boundary, then move theright
boundary leftwards. - Right to Left: If the
top
boundary is still below or equal to thebottom
, traverse thebottom
row from theright
boundary to theleft
boundary, then move thebottom
boundary upwards. - Bottom to Top: If the
left
boundary is still to the left of or equal to theright
, traverse theleft
column from thebottom
boundary to thetop
boundary, then move theleft
boundary rightwards.
- Left to Right: Traverse the
-
Repeat the above steps until the boundaries overlap, indicating that all elements have been visited.
-
Return the Result:
- Once all elements have been added to the result list, return it.
-
Time complexity:
- The time complexity is
O(M * N)
, whereM
is the number of rows andN
is the number of columns. - This is because we visit each element of the matrix exactly once.
- The time complexity is
-
Space complexity:
- The space complexity is
O(1)
if we disregard the space required for the output list. - The only extra space used is for the result list, which is proportional to the number of elements in the matrix.
- The space complexity is
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
if (matrix == null || matrix.length == 0) {
return result;
}
int top = 0;
int bottom = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
// Traverse from left to right along the top row
for (int i = left; i <= right; i++) {
result.add(matrix[top][i]);
}
top++;
// Traverse from top to bottom along the right column
for (int i = top; i <= bottom; i++) {
result.add(matrix[i][right]);
}
right--;
// Traverse from right to left along the bottom row, if still within bounds
if (top <= bottom) {
for (int i = right; i >= left; i--) {
result.add(matrix[bottom][i]);
}
bottom--;
}
// Traverse from bottom to top along the left column, if still within bounds
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result.add(matrix[i][left]);
}
left++;
}
}
return result;
}
}