-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode0054.java
74 lines (69 loc) · 2.2 KB
/
LeetCode0054.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Spiral Matrix
* Example1:
* Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
* Output: [1,2,3,6,9,8,7,4,5]
* */
package Array;
import java.util.ArrayList;
import java.util.List;
public class LeetCode0054 {
public static void main(String args[]){
int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
List output = spiralOrder(matrix);
for (int i = 0; i < output.size(); i++) {
System.out.print(output.get(i) + ",");
}
}
public static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList();
if(matrix.length == 0) //矩阵为空则返回
return res;
if(matrix.length == 1){ //矩阵只有一行
for(int i = 0; i <= matrix[0].length - 1; i ++)
res.add(matrix[0][i]);
return res;
}
if(matrix[0].length == 1){ //矩阵只有一列
for(int i = 0; i <= matrix.length - 1; i ++)
res.add(matrix[i][0]);
return res;
}
int top = 0;
int down = matrix.length - 1; //行数
int left = 0;
int right = matrix[0].length - 1; //列数
int dir = 0; //方向:0表示向右,1表示向下,2表示向左,3表示向右
while(top <= down && left <= right){
if(dir == 0){ //向右
for(int i = left; i <= right; i++)
res.add(matrix[top][i]);
top ++;
dir = 1;
}
else if(dir == 1){ //向下
for(int i = top; i <= down; i++)
res.add(matrix[i][right]);
right --;
dir = 2;
}
else if(dir == 2){ //向左
for(int i = right; i >= left; i--)
res.add(matrix[down][i]);
down --;
dir = 3;
}
else if(dir == 3){ //向上
for(int i = down; i >= top; i--)
res.add(matrix[i][left]);
left ++;
dir = 0;
}
}
return res;
}
}