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

Commit7937b87

Browse files
add one more solution for 566
1 parent53b4237 commit7937b87

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
publicclass_566 {
44

55
publicstaticclassSolution1 {
6-
publicint[][]matrixReshape(int[][]nums,intr,intc) {
7-
if (nums ==null ||nums.length ==0) {
8-
returnnums;
6+
publicint[][]matrixReshape(int[][]mat,intr,intc) {
7+
if (mat ==null ||mat.length ==0) {
8+
returnmat;
99
}
10-
intm =nums.length;
11-
intn =nums[0].length;
10+
intm =mat.length;
11+
intn =mat[0].length;
1212
if (r *c >m *n) {
13-
returnnums;
13+
returnmat;
1414
}
1515
intk =0;
1616
int[][]result =newint[r][c];
1717
for (inti =0;i <r;i++) {
1818
for (intj =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
returnresult;
2323
}
2424
}
25+
26+
publicstaticclassSolution2 {
27+
publicint[][]matrixReshape(int[][]mat,intr,intc) {
28+
intm =mat.length;
29+
intn =mat[0].length;
30+
if (m *n !=r *c) {
31+
returnmat;
32+
}
33+
int[][]reshapedMat =newint[r][c];
34+
for (inti =0;i <m;i++) {
35+
for (intj =0;j <n;j++) {
36+
intindex =i *n +j;
37+
reshapedMat[index /c][index %c] =mat[i][j];
38+
}
39+
}
40+
returnreshapedMat;
41+
}
42+
}
2543
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp