-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
30 lines (25 loc) · 903 Bytes
/
Solution.java
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
class Solution {
public int[][] spiralMatrixIII(int R, int C, int r0, int c0) {
int[][] res = new int[R * C][2],
directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int directionIndex = 0,
i = 0,
maxStep = 1,
curStep = 0;
while (i < R * C) {
if (0 <= r0 && r0 < R && 0 <= c0 && c0 < C)
res[i++] = new int[]{r0, c0};
if (curStep == maxStep)
directionIndex = (directionIndex + 1) % 4;
else if (curStep == maxStep * 2) {
directionIndex = (directionIndex + 1) % 4;
maxStep++;
curStep = 0;
}
r0 += directions[directionIndex][0];
c0 += directions[directionIndex][1];
curStep++;
}
return res;
}
}