forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution3.cs
28 lines (28 loc) · 855 Bytes
/
Solution3.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
public class Solution {
public IList<int> SpiralOrder(int[][] matrix) {
int m = matrix.Length, n = matrix[0].Length;
int x1 = 0, y1 = 0, x2 = m - 1, y2 = n - 1;
IList<int> ans = new List<int>();
while (x1 <= x2 && y1 <= y2) {
for (int j = y1; j <= y2; ++j) {
ans.Add(matrix[x1][j]);
}
for (int i = x1 + 1; i <= x2; ++i) {
ans.Add(matrix[i][y2]);
}
if (x1 < x2 && y1 < y2) {
for (int j = y2 - 1; j >= y1; --j) {
ans.Add(matrix[x2][j]);
}
for (int i = x2 - 1; i > x1; --i) {
ans.Add(matrix[i][y1]);
}
}
++x1;
++y1;
--x2;
--y2;
}
return ans;
}
}