-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiralMatrixIII.cpp
More file actions
34 lines (33 loc) · 1.09 KB
/
spiralMatrixIII.cpp
File metadata and controls
34 lines (33 loc) · 1.09 KB
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
// Source: https://leetcode.com/problems/spiral-matrix-iii/
// Author: Miao Zhang
// Date: 2021-03-21
/**********************************************************************
* 1,1,2,2,3,3,4,4,5,5
* dirs: -->
* | |
* --
**********************************************************************/
class Solution {
public:
vector<vector<int>> spiralMatrixIII(int R, int C, int r0, int c0) {
vector<vector<int>> res;
vector<vector<int>> dirs{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int d = 0; // east
int k = 0;
res.push_back({r0, c0});
if (res.size() == R * C) return res;
while (++k) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < k; j++) {
r0 += dirs[d][0];
c0 += dirs[d][1];
if (r0 < 0 || r0 >= R || c0 < 0 || c0 >= C) continue;
res.push_back({r0, c0});
if (res.size() == R * C) return res;
}
d = (d + 1) % 4;
}
}
return res;
}
};