forked from mengli/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral Matrix.java
More file actions
40 lines (36 loc) · 1004 Bytes
/
Spiral Matrix.java
File metadata and controls
40 lines (36 loc) · 1004 Bytes
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
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
You should return [1,2,3,6,9,8,7,4,5].
public class Solution {
public ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> list = new ArrayList<Integer>();
if (matrix.length == 0) return list;
int beginX = 0, endX = matrix.length - 1;
int beginY = 0, endY = matrix[0].length - 1;
while (true) {
for (int i = beginY; i <= endY; i++) {
list.add(matrix[beginX][i]);
}
if (++beginX > endX) break;
for (int i = beginX; i <= endX; i++) {
list.add(matrix[i][endY]);
}
if (beginY > --endY) break;
for (int i = endY; i >= beginY; i--) {
list.add(matrix[endX][i]);
}
if (beginX > --endX) break;
for (int i = endX; i >= beginX; i--) {
list.add(matrix[i][beginY]);
}
if (++beginY > endY) break;
}
return list;
}
}