luoheng
10/11/2019 - 8:14 AM

matrixReshape

func matrixReshape(nums [][]int, r int, c int) [][]int {
    if r > 0 && c > 0 && r * c == len(nums[0]) * len(nums) {
        newNums := make([][]int, r)
        for i := 0; i < r; i++ {
            newNums[i] = make([]int, c)
        }
        for i := 0; i < r * c; i++ {
            newNums[i/c][i%c] = nums[i/len(nums[0])][i%len(nums[0])]
        }
        return newNums
    }
    return nums
}