Skip to content

Commit 7ed3aa0

Browse files
committed
added q54
1 parent c75bd26 commit 7ed3aa0

File tree

1 file changed

+242
-0
lines changed

1 file changed

+242
-0
lines changed
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
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+
<Tabs>
73+
<TabItem value="cpp" label="C++">
74+
<SolutionAuthor name="@Vipullakum007"/>
75+
76+
```cpp
77+
78+
#include <vector>
79+
using namespace std;
80+
81+
vector<int> spiralOrder(vector<vector<int>>& matrix) {
82+
if (matrix.empty()) return {};
83+
84+
int m = matrix.size(), n = matrix[0].size();
85+
vector<int> result;
86+
int top = 0, bottom = m - 1;
87+
int left = 0, right = n - 1;
88+
89+
while (top <= bottom && left <= right) {
90+
// Traverse from left to right
91+
for (int i = left; i <= right; ++i) {
92+
result.push_back(matrix[top][i]);
93+
}
94+
++top;
95+
96+
// Traverse downwards
97+
for (int i = top; i <= bottom; ++i) {
98+
result.push_back(matrix[i][right]);
99+
}
100+
--right;
101+
102+
if (top <= bottom) {
103+
// Traverse from right to left
104+
for (int i = right; i >= left; --i) {
105+
result.push_back(matrix[bottom][i]);
106+
}
107+
--bottom;
108+
}
109+
110+
if (left <= right) {
111+
// Traverse upwards
112+
for (int i = bottom; i >= top; --i) {
113+
result.push_back(matrix[i][left]);
114+
}
115+
++left;
116+
}
117+
}
118+
119+
return result;
120+
}
121+
122+
int main() {
123+
vector<vector<int>> matrix = {
124+
{1, 2, 3, 4},
125+
{5, 6, 7, 8},
126+
{9, 10, 11, 12}
127+
};
128+
vector<int> result = spiralOrder(matrix);
129+
for (int num : result) {
130+
cout << num << " ";
131+
}
132+
return 0;
133+
}
134+
</TabItem>
135+
<TabItem value="java" label="Java">
136+
<SolutionAuthor name="@Vipullakum007"/>
137+
import java.util.ArrayList;
138+
import java.util.List;
139+
140+
public class SpiralMatrix {
141+
public List<Integer> spiralOrder(int[][] matrix) {
142+
List<Integer> result = new ArrayList<>();
143+
if (matrix.length == 0) return result;
144+
145+
int top = 0, bottom = matrix.length - 1;
146+
int left = 0, right = matrix[0].length - 1;
147+
148+
while (top <= bottom && left <= right) {
149+
for (int i = left; i <= right; i++) {
150+
result.add(matrix[top][i]);
151+
}
152+
top++;
153+
154+
for (int i = top; i <= bottom; i++) {
155+
result.add(matrix[i][right]);
156+
}
157+
right--;
158+
159+
if (top <= bottom) {
160+
for (int i = right; i >= left; i--) {
161+
result.add(matrix[bottom][i]);
162+
}
163+
bottom--;
164+
}
165+
166+
if (left <= right) {
167+
for (int i = bottom; i >= top; i--) {
168+
result.add(matrix[i][left]);
169+
}
170+
left++;
171+
}
172+
}
173+
174+
return result;
175+
}
176+
177+
public static void main(String[] args) {
178+
SpiralMatrix sm = new SpiralMatrix();
179+
int[][] matrix = {
180+
{1, 2, 3, 4},
181+
{5, 6, 7, 8},
182+
{9, 10, 11, 12}
183+
};
184+
System.out.println(sm.spiralOrder(matrix)); // Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
185+
}
186+
}
187+
</TabItem>
188+
<TabItem value="python" label="Python">
189+
<SolutionAuthor name="@Vipullakum007"/>
190+
def spiralOrder(matrix):
191+
if not matrix:
192+
return []
193+
194+
result = []
195+
top, bottom = 0, len(matrix) - 1
196+
left, right = 0, len(matrix[0]) - 1
197+
198+
while top <= bottom and left <= right:
199+
# Traverse from left to right
200+
for i in range(left, right + 1):
201+
result.append(matrix[top][i])
202+
top += 1
203+
204+
# Traverse downwards
205+
for i in range(top, bottom + 1):
206+
result.append(matrix[i][right])
207+
right -= 1
208+
209+
if top <= bottom:
210+
# Traverse from right to left
211+
for i in range(right, left - 1, -1):
212+
result.append(matrix[bottom][i])
213+
bottom -= 1
214+
215+
if left <= right:
216+
# Traverse upwards
217+
for i in range(bottom, top - 1, -1):
218+
result.append(matrix[i][left])
219+
left += 1
220+
221+
return result
222+
223+
matrix = [
224+
[1, 2, 3, 4],
225+
[5, 6, 7, 8],
226+
[9, 10, 11, 12]
227+
]
228+
print(spiralOrder(matrix)) # Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
229+
230+
</TabItem>
231+
</Tabs>
232+
Complexity Analysis
233+
Time Complexity: O(m * n)
234+
Reason: We visit every element in the matrix exactly once.
235+
236+
Space Complexity: O(1)
237+
Reason: We only use a fixed amount of extra space, regardless of the input size.
238+
References
239+
LeetCode Problem: Spiral Matrix
240+
241+
Author's LeetCode Profile: Vipul Lakum
242+
```

0 commit comments

Comments
 (0)