-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path54_spiral_matrix.java
More file actions
63 lines (52 loc) · 1.55 KB
/
54_spiral_matrix.java
File metadata and controls
63 lines (52 loc) · 1.55 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
*/
class Solution {
List<Integer> res;
public List<Integer> spiralOrder(int[][] matrix) {
res = new ArrayList<Integer>();
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return res;
}
int row_left = 0, row_right = matrix.length - 1, col_left = 0, col_right = matrix[0].length - 1;
while (row_left <= row_right && col_left <= col_right) {
for (int i = col_left;i <= col_right;i ++) {
res.add(matrix[row_left][i]);
}
for (int i = row_left + 1;i <= row_right;i ++) {
res.add(matrix[i][col_right]);
}
if (row_left != row_right) {
for (int i = col_right - 1;i > col_left;i --) {
res.add(matrix[row_right][i]);
}
}
if (col_left != col_right) {
for (int i = row_right;i > row_left;i --) {
res.add(matrix[i][col_left]);
}
}
row_left ++;
col_right --;
col_left ++;
row_right --;
}
return res;
}
}