Skip to content

Commit e3161f8

Browse files
aQuaaQua
aQua
authored and
aQua
committed
566 accepted
1 parent de07051 commit e3161f8

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
package Problem0566
22

33
func matrixReshape(nums [][]int, r int, c int) [][]int {
4+
length := len(nums) * len(nums[0])
5+
if length != r*c {
6+
return nums
7+
}
48

5-
return nil
9+
// 先降维成[]int
10+
temp := make([]int, 0, r*c)
11+
for i := range nums {
12+
temp = append(temp, nums[i]...)
13+
}
14+
15+
// 再升维
16+
res := make([][]int, r)
17+
for i := 0; i < r; i++ {
18+
res[i] = temp[i*c : (i+1)*c]
19+
}
20+
21+
return res
622
}

Algorithms/0566.reshape-the-matrix/reshape-the-matrix_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ func Test_Problem0566(t *testing.T) {
3030

3131
qs := []question{
3232

33+
question{
34+
para{[][]int{
35+
[]int{1, 2},
36+
[]int{3, 4},
37+
}, 2, 4},
38+
ans{[][]int{
39+
[]int{1, 2},
40+
[]int{3, 4},
41+
}},
42+
},
43+
3344
question{
3445
para{[][]int{
3546
[]int{1, 2},
@@ -40,6 +51,18 @@ func Test_Problem0566(t *testing.T) {
4051
}},
4152
},
4253

54+
question{
55+
para{[][]int{
56+
[]int{1, 2},
57+
[]int{3, 4},
58+
}, 4, 1},
59+
ans{[][]int{
60+
[]int{1},
61+
[]int{2},
62+
[]int{3},
63+
[]int{4},
64+
}},
65+
},
4366
// 如需多个测试,可以复制上方元素。
4467
}
4568

0 commit comments

Comments
 (0)