/
spiralMatrix
42 lines (34 loc) · 1020 Bytes
/
spiralMatrix
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) {
List<Integer> res = new ArrayList<>();
if(matrix.length == 0 )
return res;
int colstart = 0;
int colend =matrix[0].length-1;
int rowstart = 0;
int rowend = matrix.length-1;
while(rowstart <= rowend && colstart <=colend ){
for(int i = colstart ; i <=colend ; i++ ){
res.add(matrix[rowstart][i]);
}
rowstart++;
for(int i = rowstart ; i <=rowend ; i++ ){
res.add(matrix[i][colend]);
}
colend--;
if(rowstart <= rowend){
for(int j = colend ; j>=colstart ; j-- ){
res.add(matrix[rowend][j]);
}
}
rowend--;
if(colstart <= colend){
for(int j = rowend ; j>=rowstart ; j-- ){
res.add(matrix[j][colstart]);
}
}
colstart++;
}
return res;
}
}