-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cs
35 lines (35 loc) · 1010 Bytes
/
Solution.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
public class Solution {
public IList<int> SpiralOrder(int[][] matrix) {
int m = matrix.Length;
int n = matrix[0].Length;
int top = 0, bottom = m - 1, left = 0, right = n - 1;
var ans = new List<int>(m * n);
while (top <= bottom && left <= right)
{
for (int j = left; j <= right; ++j)
{
ans.Add(matrix[top][j]);
}
for (int i = top + 1; i <= bottom; ++i)
{
ans.Add(matrix[i][right]);
}
if (left < right && top < bottom)
{
for (int j = right - 1; j >= left; --j)
{
ans.Add(matrix[bottom][j]);
}
for (int i = bottom - 1; i > top; --i)
{
ans.Add(matrix[i][left]);
}
}
++top;
--bottom;
++left;
--right;
}
return ans;
}
}