-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral-matrix
More file actions
43 lines (43 loc) · 1.12 KB
/
Spiral-matrix
File metadata and controls
43 lines (43 loc) · 1.12 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
42
43
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m=matrix.length;
int n=matrix[0].length;
int id=0;
int top=0;
int down=m-1;
int left=0;
int right=n-1;
ArrayList<Integer> result=new ArrayList<>();
while(top<=down && left<=right){
if(id==0){
for(int i=left; i<=right;i++){
result.add(matrix[top][i]);
}
top++;
}
if(id==1){
for(int i=top;i<=down;i++){
result.add(matrix[i][right]);
}
right--;
}
if(id==2){
for(int i=right; i>=left; i--){
result.add(matrix[down][i]);
}
down--;
}
if(id==3){
for(int i=down; i>=top; i--){
result.add(matrix[i][left]);
}
left++;
}
id++;
if(id==4){
id=0;
}
}
return result;
}
}