-
Notifications
You must be signed in to change notification settings - Fork 0
/
54-SpiralMatrix.java
53 lines (47 loc) · 1.52 KB
/
54-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
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> ans = new ArrayList<>();
if(matrix.length==0 || matrix[0].length==0)
return ans;
int rowstart = 0;
int rowend = matrix.length-1;
int colstart = 0;
int colend = matrix[0].length-1;
int count =1;
int limit = (rowend+1)*(colend+1);
while(rowstart<=rowend && colstart<=colend && count<=limit){
//System.out.println(count);
for(int i=colstart;i<=colend;i++){
if(count>limit)
return ans;
ans.add(matrix[rowstart][i]);
count++;
}
rowstart++;
for(int i=rowstart;i<=rowend;i++){
if(count>limit)
return ans;
ans.add(matrix[i][colend]);
count++;
}
colend--;
for(int i=colend;i>=colstart;i--){
if(count>limit)
return ans;
ans.add(matrix[rowend][i]);
count++;
}
rowend--;
for(int i=rowend;i>=rowstart;i--){
if(count>limit)
return ans;
ans.add(matrix[i][colstart]);
count++;
}
colstart++;
if(rowend<rowstart || colend<colstart)
break;
}
return ans;
}
}