-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
42 lines (33 loc) · 1.08 KB
/
Copy pathSolution.java
File metadata and controls
42 lines (33 loc) · 1.08 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
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int n = matrix.length, m = matrix[0].length;
List<Integer> result = new ArrayList<>();
generate(matrix, 0, n - 1, 0, m - 1, result);
return result;
}
private static void generate(int[][] grid, int r1, int r2, int c1, int c2, List<Integer> result) {
if (r2 < r1 || c2 < c1) {
return;
}
int n = grid.length, m = grid[0].length;
for (int i = c1; i <= c2; i++) {
result.add(grid[r1][i]);
}
for (int i = r1 + 1; i < r2; i++) {
result.add(grid[i][c2]);
}
// Dont populate if the rows are the same.
if (r1 != r2) {
for (int i = c2; i >= c1; i--) {
result.add(grid[r2][i]);
}
}
// Dont populate if the columns are the same.
if (c1 != c2) {
for (int i = r2 - 1; i > r1; i--) {
result.add(grid[i][c1]);
}
}
generate(grid, r1 + 1, r2 - 1, c1 + 1, c2 - 1, result);
}
}