-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0885-SpiralMatrixIII.cs
43 lines (37 loc) · 1.26 KB
/
0885-SpiralMatrixIII.cs
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
35
36
37
38
39
40
41
42
43
//-----------------------------------------------------------------------------
// Runtime: 228ms
// Memory Usage: 30.4 MB
// Link: https://leetcode.com/submissions/detail/361649970/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0885_SpiralMatrixIII
{
public int[][] SpiralMatrixIII(int R, int C, int r0, int c0)
{
int[] dr = new int[] { 0, 1, 0, -1 };
int[] dc = new int[] { 1, 0, -1, 0 };
int[][] result = new int[R * C][];
int index = 0, direction = 0, step = 2;
result[index++] = new int[] { r0, c0 };
while (index < R * C)
{
var distance = step / 2;
for (int i = 0; i < distance; i++)
{
r0 += dr[direction];
c0 += dc[direction];
if (r0 >= 0 && r0 < R && c0 >= 0 && c0 < C)
{
result[index++] = new int[] { r0, c0 };
if (index == R * C) break;
}
}
direction++;
direction %= 4;
step++;
}
return result;
}
}
}