Skip to content

Commit de07051

Browse files
aQuaaQua
aQua
authored and
aQua
committed
566 adding
1 parent 401dcbe commit de07051

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

Algorithms/0566.reshape-the-matrix/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
# [566. Reshape the Matrix](https://leetcode.com/problems/reshape-the-matrix/)
22

33
## 题目
4+
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.
5+
6+
You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
7+
8+
The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
9+
10+
If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
11+
12+
Example 1:
13+
```
14+
Input:
15+
nums =
16+
[[1,2],
17+
[3,4]]
18+
r = 1, c = 4
19+
Output:
20+
[[1,2,3,4]]
21+
Explanation:
22+
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
23+
```
24+
Example 2:
25+
```
26+
Input:
27+
nums =
28+
[[1,2],
29+
[3,4]]
30+
r = 2, c = 4
31+
Output:
32+
[[1,2],
33+
[3,4]]
34+
Explanation:
35+
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
36+
```
37+
Note:
38+
1. The height and width of the given matrix is in range [1, 100].
39+
1. The given r and c are all positive.
440

541

642
## 解题思路
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
package Problem0566
22

3+
func matrixReshape(nums [][]int, r int, c int) [][]int {
4+
5+
return nil
6+
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0566
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -15,13 +15,14 @@ type question struct {
1515
// para 是参数
1616
// one 代表第一个参数
1717
type para struct {
18-
one string
18+
one [][]int
19+
r, c int
1920
}
2021

2122
// ans 是答案
2223
// one 代表第一个答案
2324
type ans struct {
24-
one string
25+
one [][]int
2526
}
2627

2728
func Test_Problem0566(t *testing.T) {
@@ -30,17 +31,22 @@ func Test_Problem0566(t *testing.T) {
3031
qs := []question{
3132

3233
question{
33-
para{""},
34-
ans{""},
34+
para{[][]int{
35+
[]int{1, 2},
36+
[]int{3, 4},
37+
}, 1, 4},
38+
ans{[][]int{
39+
[]int{1, 2, 3, 4},
40+
}},
3541
},
36-
42+
3743
// 如需多个测试,可以复制上方元素。
3844
}
3945

4046
for _, q := range qs {
4147
a, p := q.ans, q.para
4248
fmt.Printf("~~%v~~\n", p)
4349

44-
ast.Equal(a.one, (p.one), "输入:%v", p)
50+
ast.Equal(a.one, matrixReshape(p.one, p.r, p.c), "输入:%v", p)
4551
}
4652
}

0 commit comments

Comments
 (0)