Skip to content

Add _498.Solution2 And Test3 & 4 #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/main/java/com/fishercoder/solutions/_498.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class _498 {

public static class Solutoin1 {
Expand Down Expand Up @@ -47,4 +51,51 @@ public int[] findDiagonalOrder(int[][] matrix) {
}
}

/*
matrix = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12},
{13, 14, 15},
};
expected = new int[]{1, 2, 4, 7, 5, 3, 6, 8, 10, 13, 11, 9, 12, 14, 15};
*/
public static class Solutoin2 {
public int[] findDiagonalOrder(int[][] matrix) {
if(matrix == null || matrix.length == 0){
return new int[0];
}
List<List<Integer>> diagonals = new ArrayList<>();
int maxRow = matrix.length;
int maxCol = matrix[0].length;
int maxDiagonal = maxRow + maxCol - 1;
for (int diagonalIndex = 0; diagonalIndex < maxDiagonal; diagonalIndex++) {
int curRowIdx = (diagonalIndex < maxCol) ? 0 : (diagonalIndex - maxCol + 1);
int curColIdx = (diagonalIndex < maxCol) ? diagonalIndex : (maxCol - 1);
List<Integer> diagonal = new ArrayList<Integer>();
while (curRowIdx >= 0 && curRowIdx < maxRow && curColIdx >= 0 && curColIdx < maxCol) {
int diagonalElement = matrix[curRowIdx][curColIdx];
diagonal.add(diagonalElement);
curRowIdx++;
curColIdx--;
}
diagonals.add(diagonal);
}
int[] result = new int[maxRow * maxCol];
int resultIdx = 0;
for (int i = 0; i < diagonals.size(); i++) {
List<Integer> diagonal = diagonals.get(i);
if (i % 2 == 0) {
Collections.reverse(diagonal);
}
for (int j = 0; j < diagonal.size(); j++) {
result[resultIdx] = diagonal.get(j);
resultIdx++;
}
}
return result;
}
}

}
22 changes: 22 additions & 0 deletions src/test/java/com/fishercoder/_498Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
*/
public class _498Test {
private static _498.Solutoin1 solutoin1;
private static _498.Solutoin2 solutoin2;
private static int[][] matrix;
private static int[] expected;

@BeforeClass
public static void setup() {
solutoin1 = new _498.Solutoin1();
solutoin2 = new _498.Solutoin2();
}

@Test
Expand All @@ -42,4 +44,24 @@ public void test2() {
expected = new int[]{1, 2, 4, 7, 5, 3, 6, 8, 10, 13, 11, 9, 12, 14, 15};
assertArrayEquals(expected, solutoin1.findDiagonalOrder(matrix));
}

@Test
public void test3() {
matrix = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
};
expected = new int[]{1, 2, 4, 7, 5, 3, 6, 8, 9};
assertArrayEquals(expected, solutoin2.findDiagonalOrder(matrix));
}

@Test
public void test4() {
matrix = new int[][]{
{2,5},{8,4},{0,-1}
};
expected = new int[]{2,5,8,0,4,-1};
assertArrayEquals(expected, solutoin2.findDiagonalOrder(matrix));
}
}