-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiralbindingarray.java
More file actions
42 lines (37 loc) · 1.21 KB
/
Copy pathspiralbindingarray.java
File metadata and controls
42 lines (37 loc) · 1.21 KB
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
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class Main {
public static void main(String[] args) {
int matrix[][]={{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}};
int top=0;
int bottom=matrix.length-1;
int left=0;
int right=matrix[0].length-1;
while(top<=bottom && left<=right){
//top row
for(int i=left;i<=right;i++){
System.out.println(matrix[top][i]+ " ");
}
top++;
//right column
for(int i=top;i<=bottom;i++){
System.out.println(matrix[i][right]+ " ");
}
right--;
//bottom row
if(top<=bottom){
for(int i=right;i>=left;i--){
System.out.println(matrix[bottom][i]+ " ");
}
bottom--;
}
//left column
if(left<=right){
for(int i=bottom;i>=top;i--){
System.out.println(matrix[i][left]+ " ");
}
left++;
}
}
}
}