-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode_00054.java
More file actions
48 lines (44 loc) · 1.34 KB
/
LeetCode_00054.java
File metadata and controls
48 lines (44 loc) · 1.34 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.github.jerring.leetcode;
import java.util.ArrayList;
import java.util.List;
public class LeetCode_00054 {
public List<Integer> spiralOrder(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) {
return new ArrayList<>();
}
int top = 0, bottom = matrix.length - 1;
int left = 0, right = matrix[0].length - 1;
List<Integer> res = new ArrayList<>();
while (top <= bottom && left <= right) {
processEdge(matrix, top++, bottom--, left++, right--, res);
}
return res;
}
private void processEdge(int[][] matrix, int top, int bottom, int left, int right, List<Integer> res) {
if (top == bottom) {
for (int i = left; i <= right; ++i) {
res.add(matrix[top][i]);
}
return;
}
if (left == right) {
for (int i = top; i <= bottom; ++i) {
res.add(matrix[i][right]);
}
return;
}
int i = top, j = left;
while (j != right) {
res.add(matrix[i][j++]);
}
while (i != bottom) {
res.add(matrix[i++][j]);
}
while (j != left) {
res.add(matrix[i][j--]);
}
while (i != top) {
res.add(matrix[i--][j]);
}
}
}