-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigZagPrintMatrix.java
More file actions
37 lines (35 loc) · 1.12 KB
/
Copy pathZigZagPrintMatrix.java
File metadata and controls
37 lines (35 loc) · 1.12 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
public class ZigZagPrintMatrix {
public static void printZigZagMatrix(int[][] matrix) {
int tr = 0;
int tc = 0;
int dr = 0;
int dc = 0;
int endR = matrix.length - 1;
int endC = matrix[0].length - 1;
boolean fromUp = false;
while (tr != endR + 1) {
printLevel(matrix, fromUp, tr, tc, dr, dc);
tr = tc == endC ? tr + 1 : tr;
tc = tc == endC ? tc : tc + 1;
dc = dr == endR ? dc + 1 : dc;
dr = dr == endR ? dr : dr + 1;
fromUp = !fromUp;
}
System.out.println();
}
public static void printLevel(int[][] m, boolean direction, int tr, int tc, int dr, int dc) {
if (direction) {
while (tr != dr + 1) {
System.out.print(m[tr++][tc--] + " ");
}
} else {
while (dr != tr - 1) {
System.out.print(m[dr--][dc++] + " ");
}
}
}
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
printZigZagMatrix(matrix);
}
}