Skip to content

Commit fabd917

Browse files
authored
Add _498.Solution2 And Test3 & 4 (#131)
1 parent c0e1a67 commit fabd917

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/main/java/com/fishercoder/solutions/_498.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
37
public class _498 {
48

59
public static class Solutoin1 {
@@ -47,4 +51,51 @@ public int[] findDiagonalOrder(int[][] matrix) {
4751
}
4852
}
4953

54+
/*
55+
matrix = new int[][]{
56+
{1, 2, 3},
57+
{4, 5, 6},
58+
{7, 8, 9},
59+
{10, 11, 12},
60+
{13, 14, 15},
61+
};
62+
expected = new int[]{1, 2, 4, 7, 5, 3, 6, 8, 10, 13, 11, 9, 12, 14, 15};
63+
*/
64+
public static class Solutoin2 {
65+
public int[] findDiagonalOrder(int[][] matrix) {
66+
if(matrix == null || matrix.length == 0){
67+
return new int[0];
68+
}
69+
List<List<Integer>> diagonals = new ArrayList<>();
70+
int maxRow = matrix.length;
71+
int maxCol = matrix[0].length;
72+
int maxDiagonal = maxRow + maxCol - 1;
73+
for (int diagonalIndex = 0; diagonalIndex < maxDiagonal; diagonalIndex++) {
74+
int curRowIdx = (diagonalIndex < maxCol) ? 0 : (diagonalIndex - maxCol + 1);
75+
int curColIdx = (diagonalIndex < maxCol) ? diagonalIndex : (maxCol - 1);
76+
List<Integer> diagonal = new ArrayList<Integer>();
77+
while (curRowIdx >= 0 && curRowIdx < maxRow && curColIdx >= 0 && curColIdx < maxCol) {
78+
int diagonalElement = matrix[curRowIdx][curColIdx];
79+
diagonal.add(diagonalElement);
80+
curRowIdx++;
81+
curColIdx--;
82+
}
83+
diagonals.add(diagonal);
84+
}
85+
int[] result = new int[maxRow * maxCol];
86+
int resultIdx = 0;
87+
for (int i = 0; i < diagonals.size(); i++) {
88+
List<Integer> diagonal = diagonals.get(i);
89+
if (i % 2 == 0) {
90+
Collections.reverse(diagonal);
91+
}
92+
for (int j = 0; j < diagonal.size(); j++) {
93+
result[resultIdx] = diagonal.get(j);
94+
resultIdx++;
95+
}
96+
}
97+
return result;
98+
}
99+
}
100+
50101
}

src/test/java/com/fishercoder/_498Test.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
*/
1212
public class _498Test {
1313
private static _498.Solutoin1 solutoin1;
14+
private static _498.Solutoin2 solutoin2;
1415
private static int[][] matrix;
1516
private static int[] expected;
1617

1718
@BeforeClass
1819
public static void setup() {
1920
solutoin1 = new _498.Solutoin1();
21+
solutoin2 = new _498.Solutoin2();
2022
}
2123

2224
@Test
@@ -42,4 +44,24 @@ public void test2() {
4244
expected = new int[]{1, 2, 4, 7, 5, 3, 6, 8, 10, 13, 11, 9, 12, 14, 15};
4345
assertArrayEquals(expected, solutoin1.findDiagonalOrder(matrix));
4446
}
47+
48+
@Test
49+
public void test3() {
50+
matrix = new int[][]{
51+
{1, 2, 3},
52+
{4, 5, 6},
53+
{7, 8, 9},
54+
};
55+
expected = new int[]{1, 2, 4, 7, 5, 3, 6, 8, 9};
56+
assertArrayEquals(expected, solutoin2.findDiagonalOrder(matrix));
57+
}
58+
59+
@Test
60+
public void test4() {
61+
matrix = new int[][]{
62+
{2,5},{8,4},{0,-1}
63+
};
64+
expected = new int[]{2,5,8,0,4,-1};
65+
assertArrayEquals(expected, solutoin2.findDiagonalOrder(matrix));
66+
}
4567
}

0 commit comments

Comments
 (0)