Skip to content

Commit 695042e

Browse files
aQuaaQua
aQua
authored and
aQua
committed
566 finish
1 parent e3161f8 commit 695042e

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ Note:
3838
1. The height and width of the given matrix is in range [1, 100].
3939
1. The given r and c are all positive.
4040

41-
4241
## 解题思路
42+
新旧矩阵元素的位置,存在对应关系。依次填入即可。
4343

44-
45-
## 总结
46-
47-
44+
## 总结

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
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 {
4+
if len(nums) == 0 || len(nums[0]) == 0 || len(nums)*len(nums[0]) != r*c || len(nums) == r && len(nums[0]) == c {
65
return nums
76
}
87

9-
// 先降维成[]int
10-
temp := make([]int, 0, r*c)
11-
for i := range nums {
12-
temp = append(temp, nums[i]...)
13-
}
14-
15-
// 再升维
168
res := make([][]int, r)
17-
for i := 0; i < r; i++ {
18-
res[i] = temp[i*c : (i+1)*c]
9+
count, col := 0, len(nums[0])
10+
for i := range res {
11+
res[i] = make([]int, c)
12+
13+
for j := range res[i] {
14+
res[i][j] = nums[count/col][count%col]
15+
count++
16+
}
1917
}
2018

2119
return res

0 commit comments

Comments
 (0)