-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path059-SpiralMatrix2.cs
33 lines (28 loc) · 930 Bytes
/
059-SpiralMatrix2.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
//-----------------------------------------------------------------------------
// Runtime: 48ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _059_SpiralMatrix2
{
public int[,] GenerateMatrix(int n)
{
var result = new int[n, n];
int start = 0, end = n - 1;
int num = 1, i;
while (start < end)
{
for (i = start; i < end; i++) result[start, i] = num++;
for (i = start; i < end; i++) result[i, end] = num++;
for (i = end; i > start; i--) result[end, i] = num++;
for (i = end; i > start; i--) result[i, start] = num++;
start++;
end--;
}
if (start == end) result[start, start] = num;
return result;
}
}
}