-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralMatrix.java
45 lines (45 loc) · 1.33 KB
/
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
import java.util.*;
public class SpiralMatrix{
public static void spiral(int matrix[][]){
int startrow=0;
int endrow=matrix.length-1;
int startcol=0;
int endcol=matrix[0].length-1;
while(startrow<=endrow && startcol<=endcol){
//top
for(int j=startcol;j<=endcol;j++){
System.out.print(matrix[startrow][j] + " ");
}
//right
for(int i=startrow+1;i<=endrow;i++){
System.out.print(matrix[i][endcol]+ " ");
}
//bottom
for(int j=endcol-1;j>=startcol;j--){
if(startrow==endrow){
break;
}
System.out.print(matrix[endrow][j]+" ");
}
//forleft
for(int i=endrow-1;i>=startrow+1;i--){
if(startcol==endcol){
break;
}
System.out.print(matrix[i][startcol]+" ");
}
startcol++;
startrow++;
endcol--;
endrow--;
}
System.out.println();
}
public static void main(String args[]){
int matrix[][]={{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
spiral(matrix);
}
}