-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintMatrixSpiralOrder.java
More file actions
49 lines (45 loc) · 1.4 KB
/
Copy pathPrintMatrixSpiralOrder.java
File metadata and controls
49 lines (45 loc) · 1.4 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
43
44
45
46
47
48
49
public class PrintMatrixSpiralOrder {
public static void spiralOrderPrint(int[][] matrix) {
int tr = 0;
int tc = 0;
int dr = matrix.length - 1;
int dc = matrix[0].length - 1;
while (tr < dr) {
printEdge(matrix, tr++, tc++, dr--, dc--);
}
}
public static void printEdge(int[][] m, int tr, int tc, int dr, int dc) {
if (tr == dr) {
for (int i = tc; i <= dc; i++) {
System.out.print(m[tr][i] + " ");
}
} else if (tc == dc) {
for (int j = tr; j <= dr; j++) {
System.out.println(m[j][tc]);
}
} else {
int currC = tc;
int currR = tr;
while (currC != dc) {
System.out.print(m[tr][currC] + " ");
currC++;
}
while (currR != dr) {
System.out.print(m[currR][dc] + " ");
currR++;
}
while (currC != tc) {
System.out.print(m[dr][currC] + " ");
currC--;
}
while (currR != tr) {
System.out.print(m[currR][tc] + " ");
currR--;
}
}
}
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
spiralOrderPrint(matrix);
}
}