File tree 2 files changed +11
-16
lines changed
Algorithms/0566.reshape-the-matrix 2 files changed +11
-16
lines changed Original file line number Diff line number Diff line change 38
38
1 . The height and width of the given matrix is in range [ 1, 100] .
39
39
1 . The given r and c are all positive.
40
40
41
-
42
41
## 解题思路
42
+ 新旧矩阵元素的位置,存在对应关系。依次填入即可。
43
43
44
-
45
- ## 总结
46
-
47
-
44
+ ## 总结
Original file line number Diff line number Diff line change 1
1
package Problem0566
2
2
3
3
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 {
6
5
return nums
7
6
}
8
7
9
- // 先降维成[]int
10
- temp := make ([]int , 0 , r * c )
11
- for i := range nums {
12
- temp = append (temp , nums [i ]... )
13
- }
14
-
15
- // 再升维
16
8
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
+ }
19
17
}
20
18
21
19
return res
You can’t perform that action at this time.
0 commit comments