-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path0054-spiral-matrix.cpp
76 lines (59 loc) · 2.42 KB
/
0054-spiral-matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Problem: LeetCode 54 - Spiral Matrix
Description:
Given an m x n matrix, return all elements of the matrix in spiral order.
Intuition:
We can traverse the matrix in a spiral order by simulating the movement in four directions: right, down, left, and up.
To keep track of the visited elements, we can maintain four boundaries: top, bottom, left, and right.
Approach:
1. Initialize four variables: top = 0, bottom = m - 1, left = 0, right = n - 1.
2. Loop until top <= bottom and left <= right:
- Traverse from left to right along the top boundary and increment top.
- Traverse from top to bottom along the right boundary and decrement right.
- Traverse from right to left along the bottom boundary and decrement bottom.
- Traverse from bottom to top along the left boundary and increment left.
3. Keep adding the elements encountered during the traversal to the result vector.
Time Complexity:
The time complexity of this approach is O(m * n), where m is the number of rows and n is the number of columns in the matrix.
Space Complexity:
The space complexity is O(1) as we are not using any additional space.
*/
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>> &matrix) {
vector<int> result;
if (matrix.empty()) {
return result;
}
int m = matrix.size();
int n = matrix[0].size();
int top = 0, bottom = m - 1, left = 0, right = n - 1;
while (top <= bottom && left <= right) {
// Traverse from left to right along the top boundary
for (int i = left; i <= right; i++) {
result.push_back(matrix[top][i]);
}
top++;
// Traverse from top to bottom along the right boundary
for (int i = top; i <= bottom; i++) {
result.push_back(matrix[i][right]);
}
right--;
// Traverse from right to left along the bottom boundary
if (top <= bottom) {
for (int i = right; i >= left; i--) {
result.push_back(matrix[bottom][i]);
}
bottom--;
}
// Traverse from bottom to top along the left boundary
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result.push_back(matrix[i][left]);
}
left++;
}
}
return result;
}
};