-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral_Matrix_III.cpp
More file actions
31 lines (29 loc) · 933 Bytes
/
Spiral_Matrix_III.cpp
File metadata and controls
31 lines (29 loc) · 933 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
vector<vector<int>> result;
vector<vector<int>> directions = {{0,1}, {1,0}, {0,-1},{-1,0}};
result.push_back({rStart, cStart});
int dir = 0;
int steps = 0;
while(result.size() < rows * cols)
{
if(dir == 0 || dir == 2)
steps++;
for(int count = 0; count < steps; count++)
{
rStart += directions[dir][0];
cStart += directions[dir][1];
if(rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols)
{
result.push_back({rStart, cStart});
}
}
dir = (dir+1) % 4;
}
return result;
}
};