Skip to content

Latest commit

 

History

History
22 lines (19 loc) · 586 Bytes

566.md

File metadata and controls

22 lines (19 loc) · 586 Bytes

Reshape the Matrix

解法一

func matrixReshape(nums [][]int, r int, c int) [][]int {
    if len(nums) * len(nums[0]) != r * c || r == 0 || c == 0 || len(nums) == 0 || len(nums[0]) == 0{
        return nums
    }

    result  :=  make([][]int,r)
    count,col := 0,len(nums[0])
    for i := range result { // 这两次的构造是按照新的数据结构去构造的。
        result[i] = make([]int,c)
        for j := range result[i] {
            result[i][j] = nums[count/col][count%col] // 这里是关键
            count++
        }
    }
    return result
}