forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral Matrix.cpp
66 lines (60 loc) · 1.72 KB
/
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
class Solution {
public:
vector<int> _res;
vector<vector<bool>> _visited;
void spin(vector<vector<int>>& matrix, int direction, int i, int j) {
_visited[i][j] = true;
_res.push_back(matrix[i][j]);
switch (direction){
// left to right
case 0:
if ( j+1 >= matrix[0].size() || _visited[i][j+1]) {
direction = 1;
i++;
} else {
j++;
}
break;
// up to bottom
case 1:
if ( i+1 >= matrix.size() || _visited[i+1][j]) {
direction = 2;
j--;
} else {
i++;
}
break;
// right to left
case 2:
if ( j == 0 || _visited[i][j-1]) {
direction = 3;
i--;
} else {
j--;
}
break;
// bottom to up
case 3:
if ( i == 0 || _visited[i-1][j]) {
direction = 0;
j++;
} else {
i--;
}
break;
}
if ( i < 0 || i >= matrix.size() || j < 0 || j >= matrix[0].size() ) {
return;
}
if ( _visited[i][j] ) {
return;
}
spin(matrix, direction, i, j);
}
vector<int> spiralOrder(vector<vector<int>>& matrix) {
_res.clear();
_visited = vector<vector<bool>>(matrix.size(), std::vector<bool>(matrix[0].size(), false));
spin(matrix, 0, 0, 0);
return _res;
}
};