-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
41 lines (40 loc) · 1.24 KB
/
SpiralMatrix.java
File metadata and controls
41 lines (40 loc) · 1.24 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
package medium;
import java.util.LinkedList;
import java.util.List;
/**
*
* ClassName: SpiralMatrix
* @author chenyiAlone
* Create Time: 2019/03/27 16:19:26
* Description: No.54
* 思路:
* 1. 使用 step 用于列的更新
* 2. 使用 y 来表示 matrix 的列坐标
* 3. 循环完一层后,更改 step 的方向
*/
public class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
// int rows = matrix[0].length;
// int columns = matrix.length;
// System.out.println("rows = " + rows + " columns = " + columns);
List<Integer> ans = new LinkedList<>();
int step = 1;
int y = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
ans.add(matrix[i][y]);
if (j != matrix[i].length - 1)
y += step;
}
step = -step;
}
return ans;
}
public static void main(String[] args) {
int[][] matrix = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(new SpiralMatrix().spiralOrder(matrix));
}
}