|
| 1 | +--- |
| 2 | +id: spiral-matrix |
| 3 | +title: Spiral Matrix |
| 4 | +sidebar_label: 0054-Spiral-Matrix |
| 5 | +tags: |
| 6 | +- Array |
| 7 | +- Matrix |
| 8 | +- Simulation |
| 9 | +- C++ |
| 10 | +- Java |
| 11 | +- Python |
| 12 | +description: "This document provides a solution to the Spiral Matrix problem, where the goal is to traverse a matrix in spiral order." |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem |
| 16 | + |
| 17 | +Given an `m x n` matrix, return all elements of the matrix in spiral order. |
| 18 | + |
| 19 | +### Example 1: |
| 20 | + |
| 21 | +Input: |
| 22 | +matrix = [ |
| 23 | +[ 1, 2, 3 ], |
| 24 | +[ 4, 5, 6 ], |
| 25 | +[ 7, 8, 9 ] |
| 26 | +] |
| 27 | + |
| 28 | +Output: |
| 29 | +[1, 2, 3, 6, 9, 8, 7, 4, 5] |
| 30 | + |
| 31 | +### Example 2: |
| 32 | + |
| 33 | +Input: |
| 34 | +matrix = [ |
| 35 | +[1, 2, 3, 4], |
| 36 | +[5, 6, 7, 8], |
| 37 | +[9, 10, 11, 12] |
| 38 | +] |
| 39 | + |
| 40 | +Output: |
| 41 | +[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] |
| 42 | + |
| 43 | +### Constraints: |
| 44 | + |
| 45 | +- `m == matrix.length` |
| 46 | +- `n == matrix[i].length` |
| 47 | +- `1 <= m, n <= 10` |
| 48 | +- `-100 <= matrix[i][j] <= 100` |
| 49 | + |
| 50 | +## Solution |
| 51 | + |
| 52 | +To solve this problem, we need to simulate the traversal of the matrix in spiral order. We can define four boundaries to keep track of the rows and columns that we have already traversed: `top`, `bottom`, `left`, and `right`. We start at the top-left corner and move in the following order: |
| 53 | + |
| 54 | +1. Traverse from left to right. |
| 55 | +2. Traverse downwards. |
| 56 | +3. Traverse from right to left. |
| 57 | +4. Traverse upwards. |
| 58 | + |
| 59 | +We continue this process while adjusting the boundaries until all elements have been traversed. |
| 60 | + |
| 61 | +### Step-by-Step Approach |
| 62 | + |
| 63 | +1. Initialize `top`, `bottom`, `left`, and `right` boundaries. |
| 64 | +2. Use a loop to traverse the matrix until all elements are visited: |
| 65 | + - Traverse from left to right within the `top` boundary and increment `top`. |
| 66 | + - Traverse downwards within the `right` boundary and decrement `right`. |
| 67 | + - Traverse from right to left within the `bottom` boundary and decrement `bottom`. |
| 68 | + - Traverse upwards within the `left` boundary and increment `left`. |
| 69 | + |
| 70 | +### Code in Different Languages |
| 71 | + |
| 72 | +### C++ Solution |
| 73 | + |
| 74 | +```cpp |
| 75 | + |
| 76 | +#include <vector> |
| 77 | +using namespace std; |
| 78 | + |
| 79 | +vector<int> spiralOrder(vector<vector<int>>& matrix) { |
| 80 | + if (matrix.empty()) return {}; |
| 81 | + |
| 82 | + int m = matrix.size(), n = matrix[0].size(); |
| 83 | + vector<int> result; |
| 84 | + int top = 0, bottom = m - 1; |
| 85 | + int left = 0, right = n - 1; |
| 86 | + |
| 87 | + while (top <= bottom && left <= right) { |
| 88 | + // Traverse from left to right |
| 89 | + for (int i = left; i <= right; ++i) { |
| 90 | + result.push_back(matrix[top][i]); |
| 91 | + } |
| 92 | + ++top; |
| 93 | + |
| 94 | + // Traverse downwards |
| 95 | + for (int i = top; i <= bottom; ++i) { |
| 96 | + result.push_back(matrix[i][right]); |
| 97 | + } |
| 98 | + --right; |
| 99 | + |
| 100 | + if (top <= bottom) { |
| 101 | + // Traverse from right to left |
| 102 | + for (int i = right; i >= left; --i) { |
| 103 | + result.push_back(matrix[bottom][i]); |
| 104 | + } |
| 105 | + --bottom; |
| 106 | + } |
| 107 | + |
| 108 | + if (left <= right) { |
| 109 | + // Traverse upwards |
| 110 | + for (int i = bottom; i >= top; --i) { |
| 111 | + result.push_back(matrix[i][left]); |
| 112 | + } |
| 113 | + ++left; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return result; |
| 118 | +} |
| 119 | + |
| 120 | +int main() { |
| 121 | + vector<vector<int>> matrix = { |
| 122 | + {1, 2, 3, 4}, |
| 123 | + {5, 6, 7, 8}, |
| 124 | + {9, 10, 11, 12} |
| 125 | + }; |
| 126 | + vector<int> result = spiralOrder(matrix); |
| 127 | + for (int num : result) { |
| 128 | + cout << num << " "; |
| 129 | + } |
| 130 | + return 0; |
| 131 | +} |
| 132 | +</TabItem> |
| 133 | +``` |
| 134 | +### JAVA Solution |
| 135 | +```java |
| 136 | +import java.util.ArrayList; |
| 137 | +import java.util.List; |
| 138 | +
|
| 139 | +public class SpiralMatrix { |
| 140 | + public List<Integer> spiralOrder(int[][] matrix) { |
| 141 | + List<Integer> result = new ArrayList<>(); |
| 142 | + if (matrix.length == 0) return result; |
| 143 | +
|
| 144 | + int top = 0, bottom = matrix.length - 1; |
| 145 | + int left = 0, right = matrix[0].length - 1; |
| 146 | +
|
| 147 | + while (top <= bottom && left <= right) { |
| 148 | + for (int i = left; i <= right; i++) { |
| 149 | + result.add(matrix[top][i]); |
| 150 | + } |
| 151 | + top++; |
| 152 | +
|
| 153 | + for (int i = top; i <= bottom; i++) { |
| 154 | + result.add(matrix[i][right]); |
| 155 | + } |
| 156 | + right--; |
| 157 | +
|
| 158 | + if (top <= bottom) { |
| 159 | + for (int i = right; i >= left; i--) { |
| 160 | + result.add(matrix[bottom][i]); |
| 161 | + } |
| 162 | + bottom--; |
| 163 | + } |
| 164 | +
|
| 165 | + if (left <= right) { |
| 166 | + for (int i = bottom; i >= top; i--) { |
| 167 | + result.add(matrix[i][left]); |
| 168 | + } |
| 169 | + left++; |
| 170 | + } |
| 171 | + } |
| 172 | +
|
| 173 | + return result; |
| 174 | + } |
| 175 | +
|
| 176 | + public static void main(String[] args) { |
| 177 | + SpiralMatrix sm = new SpiralMatrix(); |
| 178 | + int[][] matrix = { |
| 179 | + {1, 2, 3, 4}, |
| 180 | + {5, 6, 7, 8}, |
| 181 | + {9, 10, 11, 12} |
| 182 | + }; |
| 183 | + System.out.println(sm.spiralOrder(matrix)); // Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] |
| 184 | + } |
| 185 | +} |
| 186 | +
|
| 187 | +``` |
| 188 | +### Python Solution |
| 189 | + |
| 190 | +```python |
| 191 | + def spiralOrder(matrix): |
| 192 | + if not matrix: |
| 193 | + return [] |
| 194 | + |
| 195 | + result = [] |
| 196 | + top, bottom = 0, len(matrix) - 1 |
| 197 | + left, right = 0, len(matrix[0]) - 1 |
| 198 | + |
| 199 | + while top <= bottom and left <= right: |
| 200 | + # Traverse from left to right |
| 201 | + for i in range(left, right + 1): |
| 202 | + result.append(matrix[top][i]) |
| 203 | + top += 1 |
| 204 | + |
| 205 | + # Traverse downwards |
| 206 | + for i in range(top, bottom + 1): |
| 207 | + result.append(matrix[i][right]) |
| 208 | + right -= 1 |
| 209 | + |
| 210 | + if top <= bottom: |
| 211 | + # Traverse from right to left |
| 212 | + for i in range(right, left - 1, -1): |
| 213 | + result.append(matrix[bottom][i]) |
| 214 | + bottom -= 1 |
| 215 | + |
| 216 | + if left <= right: |
| 217 | + # Traverse upwards |
| 218 | + for i in range(bottom, top - 1, -1): |
| 219 | + result.append(matrix[i][left]) |
| 220 | + left += 1 |
| 221 | + |
| 222 | + return result |
| 223 | + |
| 224 | +matrix = [ |
| 225 | + [1, 2, 3, 4], |
| 226 | + [5, 6, 7, 8], |
| 227 | + [9, 10, 11, 12] |
| 228 | +] |
| 229 | +print(spiralOrder(matrix)) # Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] |
| 230 | + |
| 231 | +``` |
| 232 | + |
| 233 | +### Complexity Analysis |
| 234 | +#### Time Complexity: O(m * n) |
| 235 | +>Reason: We visit every element in the matrix exactly once. |
| 236 | +
|
| 237 | +**Space Complexity:** O(1) |
| 238 | +>Reason: We only use a fixed amount of extra space, regardless of the input size. |
| 239 | +### References |
| 240 | +**LeetCode Problem:** Spiral Matrix |
| 241 | + |
| 242 | + |
0 commit comments