-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path885.cpp
More file actions
22 lines (22 loc) · 744 Bytes
/
885.cpp
File metadata and controls
22 lines (22 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
vector<vector<int>> res;
int index = 2;
int directionIdx = 0;
int x = rStart;
int y = cStart;
while (res.size() < rows * cols) {
int move = index / 2;
while (move--) {
if (x >= 0 && x < rows && y >= 0 && y < cols) res.push_back({x, y});
x += directions[directionIdx].first;
y += directions[directionIdx].second;
}
index++;
directionIdx = (directionIdx + 1) % 4;
}
return res;
}
};