From f732de48c0d2df779a4b3e605be3efc7adb77b99 Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Wed, 25 Aug 2021 15:52:11 +0800 Subject: [PATCH 1/2] feat: add java solution to lc problem: NO.1329. Sort the Matrix Diagonally --- .../1329.Sort the Matrix Diagonally/README.md | 24 ++++++++++++++++++- .../Solution.java | 23 ++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 solution/1300-1399/1329.Sort the Matrix Diagonally/Solution.java diff --git a/solution/1300-1399/1329.Sort the Matrix Diagonally/README.md b/solution/1300-1399/1329.Sort the Matrix Diagonally/README.md index 525ce6f957fd7..67059d30235ac 100644 --- a/solution/1300-1399/1329.Sort the Matrix Diagonally/README.md +++ b/solution/1300-1399/1329.Sort the Matrix Diagonally/README.md @@ -59,7 +59,29 @@ ```java - +class Solution { + public int[][] diagonalSort(int[][] mat) { + for (int i = 0; i < mat.length; i++) { + handler(mat, i, 0); + } + for (int i = 0; i < mat[0].length; i++){ + handler(mat, 0, i); + } + return mat; + } + + public void handler(int[][] mat, int i, int j) { + for (; i < mat.length && j < mat[0].length; i++, j++) { + for (int k = i + 1, p = j + 1; k < mat.length && p < mat[0].length; k++, p++) { + if (mat[k][p] < mat[i][j]){ + int temp = mat[k][p]; + mat[k][p] = mat[i][j]; + mat[i][j] = temp; + } + } + } + } +} ``` ### **...** diff --git a/solution/1300-1399/1329.Sort the Matrix Diagonally/Solution.java b/solution/1300-1399/1329.Sort the Matrix Diagonally/Solution.java new file mode 100644 index 0000000000000..ff79bbc2c608a --- /dev/null +++ b/solution/1300-1399/1329.Sort the Matrix Diagonally/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public int[][] diagonalSort(int[][] mat) { + for (int i = 0; i < mat.length; i++) { + handler(mat, i, 0); + } + for (int i = 0; i < mat[0].length; i++){ + handler(mat, 0, i); + } + return mat; + } + + public void handler(int[][] mat, int i, int j) { + for ( ; i < mat.length && j < mat[0].length; i++, j++) { + for (int k = i + 1, p = j + 1; k < mat.length && p < mat[0].length; k++, p++) { + if (mat[k][p] < mat[i][j]){ + int temp = mat[k][p]; + mat[k][p] = mat[i][j]; + mat[i][j] = temp; + } + } + } + } +} \ No newline at end of file From b34b64bea816ace389c8220202efce7c25c6c890 Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Wed, 25 Aug 2021 16:19:05 +0800 Subject: [PATCH 2/2] feat: add java solution to lc problem: NO.1329. Sort the Matrix Diagonally --- .../1329.Sort the Matrix Diagonally/README.md | 2 +- .../README_EN.md | 24 ++++++++++++++++++- .../Solution.java | 4 ++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/solution/1300-1399/1329.Sort the Matrix Diagonally/README.md b/solution/1300-1399/1329.Sort the Matrix Diagonally/README.md index 67059d30235ac..cfded96994c1b 100644 --- a/solution/1300-1399/1329.Sort the Matrix Diagonally/README.md +++ b/solution/1300-1399/1329.Sort the Matrix Diagonally/README.md @@ -64,7 +64,7 @@ class Solution { for (int i = 0; i < mat.length; i++) { handler(mat, i, 0); } - for (int i = 0; i < mat[0].length; i++){ + for (int i = 0; i < mat[0].length; i++) { handler(mat, 0, i); } return mat; diff --git a/solution/1300-1399/1329.Sort the Matrix Diagonally/README_EN.md b/solution/1300-1399/1329.Sort the Matrix Diagonally/README_EN.md index 45999386ad51e..ff30fba70e0d5 100644 --- a/solution/1300-1399/1329.Sort the Matrix Diagonally/README_EN.md +++ b/solution/1300-1399/1329.Sort the Matrix Diagonally/README_EN.md @@ -47,7 +47,29 @@ ### **Java** ```java - +class Solution { + public int[][] diagonalSort(int[][] mat) { + for (int i = 0; i < mat.length; i++) { + handler(mat, i, 0); + } + for (int i = 0; i < mat[0].length; i++) { + handler(mat, 0, i); + } + return mat; + } + + public void handler(int[][] mat, int i, int j) { + for (; i < mat.length && j < mat[0].length; i++, j++) { + for (int k = i + 1, p = j + 1; k < mat.length && p < mat[0].length; k++, p++) { + if (mat[k][p] < mat[i][j]){ + int temp = mat[k][p]; + mat[k][p] = mat[i][j]; + mat[i][j] = temp; + } + } + } + } +} ``` ### **...** diff --git a/solution/1300-1399/1329.Sort the Matrix Diagonally/Solution.java b/solution/1300-1399/1329.Sort the Matrix Diagonally/Solution.java index ff79bbc2c608a..91214d7df3fa1 100644 --- a/solution/1300-1399/1329.Sort the Matrix Diagonally/Solution.java +++ b/solution/1300-1399/1329.Sort the Matrix Diagonally/Solution.java @@ -3,14 +3,14 @@ public int[][] diagonalSort(int[][] mat) { for (int i = 0; i < mat.length; i++) { handler(mat, i, 0); } - for (int i = 0; i < mat[0].length; i++){ + for (int i = 0; i < mat[0].length; i++) { handler(mat, 0, i); } return mat; } public void handler(int[][] mat, int i, int j) { - for ( ; i < mat.length && j < mat[0].length; i++, j++) { + for (; i < mat.length && j < mat[0].length; i++, j++) { for (int k = i + 1, p = j + 1; k < mat.length && p < mat[0].length; k++, p++) { if (mat[k][p] < mat[i][j]){ int temp = mat[k][p];