File tree Expand file tree Collapse file tree 1 file changed +25
-7
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +25
-7
lines changed Original file line number Diff line number Diff line change 3
3
public class _566 {
4
4
5
5
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 ;
9
9
}
10
- int m = nums .length ;
11
- int n = nums [0 ].length ;
10
+ int m = mat .length ;
11
+ int n = mat [0 ].length ;
12
12
if (r * c > m * n ) {
13
- return nums ;
13
+ return mat ;
14
14
}
15
15
int k = 0 ;
16
16
int [][] result = new int [r ][c ];
17
17
for (int i = 0 ; i < r ; i ++) {
18
18
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 ];
20
20
}
21
21
}
22
22
return result ;
23
23
}
24
24
}
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
+ }
25
43
}
You can’t perform that action at this time.
0 commit comments