-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
70 lines (62 loc) · 1.66 KB
/
Copy pathSpiralMatrix.java
File metadata and controls
70 lines (62 loc) · 1.66 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
64
65
66
67
68
69
package Lists;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @description: 描述 Medium
* @author: dekai.kong
* @date: 2018-12-13 10:53
* @from
*/
public class SpiralMatrix {
public SpiralMatrix() {
}
/**
* Runtime: 1 ms, faster than 100.00% of Java online submissions for Spiral Matrix.
* @param matrix
* @return
* 就是暴力破解,上右下左-->上右下左的循环
*/
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
if(matrix == null||matrix.length == 0||matrix[0].length == 0||matrix[0] == null){
return list;
}
int rows = 0;
int rowe = matrix.length-1;
int cols = 0;
int cole = matrix[0].length -1;
while(rows<=rowe && cols<= cole){
for (int i = cols; i <= cole; i++) {
list.add(matrix[rows][i]);
}
rows++;
for (int i = rows; i <= rowe; i++) {
list.add(matrix[i][cole]);
}
cole--;
if(rows<=rowe){
for (int i = cole; i >= cols; i--) {
list.add(matrix[rowe][i]);
}
}
rowe --;
if(cols<=cole){
for (int i = rowe; i >=rows ; i--) {
list.add(matrix[i][cols]);
}
}
cols++;
}
System.out.println(list);
return list;
}
@Test
public void test() {
spiralOrder(new int[][]{
{1,2,3},
{4,5,6},
{7,8,9}
});
}
}