-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay_7_Spiral_Matrix.java
44 lines (42 loc) · 1.25 KB
/
Day_7_Spiral_Matrix.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
import java.util.ArrayList;
public class Day_7_Spiral_Matrix{
public static void main(String[] args) {
int arr[][]={{11,12,13,50},{14,15,16,51},{17,18,19,53},{10,11,12,55},{13,14,15,54}};
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
ArrayList <Integer> li=new ArrayList<>();
int top=0;
int left=0;
int right=arr[0].length-1; //3
int bottom=arr.length-1; //4
System.out.println(right);
while(left<=right && top<=bottom){
for(int i=left;i<=right;i++) {
li.add(arr[top][i]);
}
top++;
for(int i=top;i<=bottom;i++){
li.add(arr[i][right]);
}
right--;
if(top<=bottom){
for(int i=right;i>=left;i--){
li.add(arr[bottom][i]);
}
bottom--;
}
if(left<=right)
{
for(int i=bottom;i>=top;i--){
li.add(arr[i][left]);
}
left++;
}
}
System.out.println(li);
}
}