-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral Matrix-54
44 lines (36 loc) · 1.08 KB
/
Spiral Matrix-54
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
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m=matrix.length;
List<Integer>res=new ArrayList<>();
if(m==0)
return res;
int n=matrix[0].length;
int rowstart=0;
int rowend=m;
int colstart=0;
int colend=n;
int k;
while(rowstart<rowend && colstart<colend)
{
for(k=colstart;k<colend;k++)
res.add(matrix[rowstart][k]);
rowstart+=1;
for(k=rowstart;k<rowend;k++)
res.add(matrix[k][colend-1]);
colend-=1;
if(rowstart<rowend)
{
for(k=colend-1;k>=colstart;k--)
res.add(matrix[rowend-1][k]);
rowend-=1;
}
if(colstart<colend)
{
for(k=rowend-1;k>=rowstart;k--)
res.add(matrix[k][colstart]);
colstart+=1;
}
}
return res;
}
}