-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalTraversal.java
More file actions
65 lines (57 loc) · 1.66 KB
/
DiagonalTraversal.java
File metadata and controls
65 lines (57 loc) · 1.66 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ArraysD2;
public class DiagonalTraversal {
static int[] getDiagonal(int[][] matrix, int r, int c) {
int[] ans = new int[r * c];
int index = 0, row = 0, col = 0;
while (col < c) {
int i = row, j = col;
while (i >= 0 && j < c) {
ans[index++] = matrix[i--][j++];
}
row++;
if (row >= r) {
row = r - 1;
col++;
}
}
return ans;
}
public static int[] matrixDiagonally(int[][] mat) {
int n = mat.length;
int[] res = new int[n * n];
int row = 0, col = 0;
for (int i = 0; i < n * n; i++) {
res[i] = mat[row][col];
// If sum of row and col is even, move up-right.
if ((row + col) % 2 == 0) {
if (col == n - 1) {
row++;
} else if (row == 0) {
col++;
} else {
row--;
col++;
}
}
// If sum of row and col is odd, move down-left.
else {
if (row == n - 1) {
col++;
} else if (col == 0) {
row++;
} else {
row++;
col--;
}
}
}
return res;
}
public static void main(String[] args) {
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[] diagonal = getDiagonal(arr, arr.length, arr[0].length);
for (int num : diagonal) {
System.out.print(num + " ");
}
}
}