-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspiral matrix
34 lines (34 loc) · 1.04 KB
/
spiral matrix
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
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int top=0;
int left=0;
int right= matrix[0].length-1;
int bottom= matrix.length-1;
List<Integer> list= new ArrayList<>();
int val= (right+1)*(bottom+1);
int count=0;
while(val!=count){
for(int i=left;i<=right;i++){
list.add(matrix[top][i]);
count++;
if(val==count) return list;
}top++;
for(int i=top;i<=bottom;i++){
list.add(matrix[i][right]);
count++;
if(val==count) return list;
}right--;
for(int i=right;i>=left;i--){
list.add(matrix[bottom][i]);
count++;
if(val==count) return list;
}bottom--;
for(int i=bottom;i>=top;i--){
list.add(matrix[i][left]);
count++;
if(val==count) return list;
}left++;
}
return list;
}
}