-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path885.cpp
More file actions
20 lines (20 loc) · 695 Bytes
/
Copy path885.cpp
File metadata and controls
20 lines (20 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
// Runtime: 52 ms, faster than 99.27% of C++ online submissions for Spiral
// Matrix III. Memory Usage: 13.6 MB, less than 60.22% of C++ online
// submissions for Spiral Matrix III.
public:
vector<vector<int>> spiralMatrixIII(int R, int C, int r, int c) {
vector<vector<int>> res = {{r, c}};
vector<pair<int, int>> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int curDir = 0;
for (int n = 0; res.size() < R * C; ++n) {
auto [dirX, dirY] = dirs[curDir++ % 4];
for (int i = 0; i < n / 2 + 1; ++i) {
r += dirY, c += dirX;
if (0 <= r && r < R && 0 <= c && c < C)
res.push_back({r, c});
}
}
return res;
}
};