-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathSpiral_matrix.java
40 lines (40 loc) · 1.11 KB
/
Spiral_matrix.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
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int minr = 0;
int minc = 0;
int m = matrix.length;
int n = matrix[0].length;
int maxr = m-1;
int maxc = n-1;
int tne = m * n;
int cnt = 0;
ArrayList one = new ArrayList();
while(cnt<tne){
//top wall
for(int i = minr, j = minc; j <= maxc && cnt < tne; j++){
one.add(matrix[i][j]);
cnt++;
}
minr++;
//rgt wall
for(int i = minr, j = maxc; i <= maxr && cnt < tne; i++){
one.add(matrix[i][j]);
cnt++;
}
maxc--;
//btm wall
for(int i = maxr, j = maxc; j >= minc && cnt < tne; j--){
one.add(matrix[i][j]);
cnt++;
}
maxr--;
//lft wall
for(int i = maxr, j = minc; i >= minr && cnt < tne; i--){
one.add(matrix[i][j]);
cnt++;
}
minc++;
}
return one;
}
}