-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathSpiralMatrix.java
61 lines (57 loc) · 1.84 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*;
public class SpiralMatrix {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int m = sc.nextInt();
System.out.print("Enter the number of columns: ");
int n = sc.nextInt();
int matrix[][] = new int[m][n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = sc.nextInt();
}
System.out.println();
}
System.out.println(spiralOrder(matrix));
sc.close();
}
public static List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
int minr = 0;
int minc = 0;
int maxr = matrix.length - 1;
int maxc = matrix[0].length - 1;
int total = matrix.length * matrix[0].length;
;
int count = 1;
while (count <= total) {
// traverse upperwall
for (int i = minc; i <= maxc && count <= total; i++) {
list.add(matrix[minr][i]);
count++;
}
minr++;
// traverse right wall
for (int i = minr; i <= maxr && count <= total; i++) {
list.add(matrix[i][maxc]);
count++;
}
maxc--;
// traverse lower wall
for (int i = maxc; i >= minc && count <= total; i--) {
list.add(matrix[maxr][i]);
count++;
}
maxr--;
// traverse left wall
for (int i = maxr; i >= minr && count <= total; i--) {
list.add(matrix[i][minc]);
count++;
}
minc++;
}
return list;
}
}