Skip to content

Commit 7937b87

Browse files
add one more solution for 566
1 parent 53b4237 commit 7937b87

File tree

1 file changed

+25
-7
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+25
-7
lines changed

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,41 @@
33
public class _566 {
44

55
public static class Solution1 {
6-
public int[][] matrixReshape(int[][] nums, int r, int c) {
7-
if (nums == null || nums.length == 0) {
8-
return nums;
6+
public int[][] matrixReshape(int[][] mat, int r, int c) {
7+
if (mat == null || mat.length == 0) {
8+
return mat;
99
}
10-
int m = nums.length;
11-
int n = nums[0].length;
10+
int m = mat.length;
11+
int n = mat[0].length;
1212
if (r * c > m * n) {
13-
return nums;
13+
return mat;
1414
}
1515
int k = 0;
1616
int[][] result = new int[r][c];
1717
for (int i = 0; i < r; i++) {
1818
for (int j = 0; j < c; j++, k++) {
19-
result[i][j] = nums[k / n][k % n];
19+
result[i][j] = mat[k / n][k % n];
2020
}
2121
}
2222
return result;
2323
}
2424
}
25+
26+
public static class Solution2 {
27+
public int[][] matrixReshape(int[][] mat, int r, int c) {
28+
int m = mat.length;
29+
int n = mat[0].length;
30+
if (m * n != r * c) {
31+
return mat;
32+
}
33+
int[][] reshapedMat = new int[r][c];
34+
for (int i = 0; i < m; i++) {
35+
for (int j = 0; j < n; j++) {
36+
int index = i * n + j;
37+
reshapedMat[index / c][index % c] = mat[i][j];
38+
}
39+
}
40+
return reshapedMat;
41+
}
42+
}
2543
}

0 commit comments

Comments
 (0)