-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix_54.java
More file actions
50 lines (45 loc) · 1.37 KB
/
Copy pathSpiralMatrix_54.java
File metadata and controls
50 lines (45 loc) · 1.37 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
package Array;
import java.util.LinkedList;
import java.util.List;
/**
* 圆圈打印数组
*/
public class SpiralMatrix_54 {
public List<Integer> spiralOrder(int[][] matrix) {
if(matrix==null) return new LinkedList<>();
int rows=matrix.length;
if(rows==0) return new LinkedList<>();
int cols=matrix[0].length;
return spiralOrderPrint(matrix,rows,cols);
}
private List<Integer> spiralOrderPrint(int[][] matrix, int rows, int cols) {
List<Integer> result=new LinkedList<>();
int start=0;
while(rows>start*2&&cols>start*2){
int endX=cols-1-start;
int endY=rows-1-start;
//从左到右
for(int i=start;i<=endX;i++){
result.add(matrix[start][i]);
}
//从上到下
if(start<endY){
for(int i=start+1;i<=endY;i++)
result.add(matrix[i][endX]);
}
//从右到左
if(start<endY&&start<endX){
for(int i=endX-1;i>=start;--i){
result.add(matrix[endY][i]);
}
}
//从下到上
if(start<endY-1&&start<endX){
for(int i=endY-1;i>start;--i)
result.add(matrix[i][start]);
}
start++;
}
return result;
}
}