-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
32 lines (32 loc) · 988 Bytes
/
SpiralMatrix.java
File metadata and controls
32 lines (32 loc) · 988 Bytes
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
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int rows=matrix.length;
int cols=matrix[0].length;
int startrow=0;
int endrow=rows-1;
int startcol=0;
int endcol=cols-1;
List<Integer> sol=new ArrayList<>();
while(startrow<=endrow && startcol<=endcol){
for(int i=startcol;i<=endcol;i++){
sol.add(matrix[startrow][i]);
}
for(int i=startrow+1;i<=endrow;i++){
sol.add(matrix[i][endcol]);
}
if(endrow>startrow && endcol>startcol){
for(int i=endcol-1;i>=startcol;i--){
sol.add(matrix[endrow][i]);
}
for(int i=endrow-1;i>startrow;i--){
sol.add(matrix[i][startcol]);
}
}
startrow++;
endcol--;
endrow--;
startcol++;
}
return sol;
}
}