forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralMatrix.java
69 lines (61 loc) · 1.52 KB
/
SpiralMatrix.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
package com.stevesun.solutions;
import java.util.ArrayList;
import java.util.List;
/**
* 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 SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> res = new ArrayList();
int row = matrix.length;
if(row == 0){
return res;
}
int col = matrix[0].length;
int len = row*col;
int i = 0, j = 0, rowStart = 1, colStart = 0;
while(res.size() <= len){
for(; j < col; j++){
res.add(matrix[i][j]);
}
if(res.size() == len)
break;
col--;
j--;
i++;
for(; i < row; i++){
res.add(matrix[i][j]);
}
if(res.size() == len)
break;
row--;
i--;
j--;
for(; j >= colStart; j--){
res.add(matrix[i][j]);
}
if(res.size() == len)
break;
colStart++;
j++;
i--;
for(; i >= rowStart; i--){
res.add(matrix[i][j]);
}
if(res.size() == len)
break;
rowStart++;
i ++;
j++;
}
return res;
}
}