/
Spiral_Matrix.java
51 lines (41 loc) · 1.65 KB
/
Spiral_Matrix.java
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
package OJ;
import java.util.ArrayList;
import java.util.List;
/**
* Created by arachis on 2017/4/11.
* 给定一个矩阵,返回顺时针顺序重新构成的数组
* 上右下左四个边分别变化,每遍历完一个边都要判断下是否已经遍历过
*/
public class Spiral_Matrix {
//上右下左四个边分别变化,每遍历完一个边都要判断下是否已经遍历过
public List<Integer> generateMatrix(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
if(matrix.length == 0 || matrix[0].length == 0) return res;
int top = 0;
int bottom = matrix.length-1;
int left = 0;
int right = matrix[0].length-1;
while(true){
for(int i = left; i <= right; i++) res.add(matrix[top][i]);
top+=1;
if(left > right || top > bottom) break;
for(int i = top; i <= bottom; i++) res.add(matrix[i][right]);
right-=1;
if(left > right || top > bottom) break;
for(int i = right; i >= left; i--) res.add(matrix[bottom][i]);
bottom-=1;
if(left > right || top > bottom) break;
for(int i = bottom; i >= top; i--) res.add(matrix[i][left]);
left+=1;
if(left > right || top > bottom) break;
}
return res;
}
public static void main(String[] args) {
int[][] nums = {{1,2,3},{8,9,4},{7,6,5}};
List<Integer> list = new Spiral_Matrix().generateMatrix(nums);
for( int i: list ){
System.out.println(i);
}
}
}