Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fishercoder1534 merged 1 commit intofishercoder1534:masterfromWillShen7789:master
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionssrc/main/java/com/fishercoder/solutions/_498.java
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionssrc/test/java/com/fishercoder/_498Test.java
View file
Open in desktop
Original file line numberDiff line numberDiff 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));
}
}

[8]ページ先頭

©2009-2025 Movatter.jp