-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution885.cs
52 lines (45 loc) · 1.18 KB
/
Solution885.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
44
45
46
47
48
49
50
51
52
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution885
{
/// <summary>
/// 885. Spiral Matrix III - Medium
/// <a href="https://leetcode.com/problems/spiral-matrix-iii">See the problem</a>
/// </summary>
public int[][] SpiralMatrixIII(int rows, int cols, int rStart, int cStart)
{
var result = new List<int[]>();
int r = rStart;
int c = cStart;
int step = 1;
int steps = 1;
int direction = 0;
int[][] directions =
[
[0, 1],
[1, 0],
[0, -1],
[-1, 0]
];
while (result.Count < rows * cols)
{
for (int i = 0; i < steps; i++)
{
if (r >= 0 && r < rows && c >= 0 && c < cols)
{
result.Add([r, c]);
}
r += directions[direction][0];
c += directions[direction][1];
}
if (step % 2 == 0)
{
steps++;
}
direction = (direction + 1) % 4;
step++;
}
return [.. result];
}
}