-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
28 lines (28 loc) · 925 Bytes
/
Solution.java
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
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int top = 0, bottom = m - 1, left = 0, right = n - 1;
List<Integer> ans = new ArrayList<>();
while (left <= right && top <= bottom) {
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;
}
}