-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL00054.java
More file actions
28 lines (26 loc) · 788 Bytes
/
L00054.java
File metadata and controls
28 lines (26 loc) · 788 Bytes
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
package leetcode;
import java.util.*;
public class L00054 {
public List<Integer> spiralOrder(int[][] matrix) {
int h = matrix.length - 1, w = matrix[0].length - 1;
ArrayList<Integer> ans = new ArrayList<Integer>();
int r = 0, c = 0;
while (r <= h && c <= w) {
for (int i = c; i <= w; i++)
ans.add(matrix[r][i]);
r++;
for (int i = r; i <= h; i++)
ans.add(matrix[i][w]);
w--;
if (r <= h)
for (int i = w; i >= c; i--)
ans.add(matrix[h][i]);
h--;
if (c <= w)
for (int i = h; i >= r; i--)
ans.add(matrix[i][c]);
c++;
}
return ans;
}
}