This is a creation in Article, where the information may have evolved or changed.
The practice of writing this problem with Golang is completely different, using the Gorutine, channel, writing a very interesting
Title Description:
leetcode 566. Reshape The Matrix
in MATLAB, there is a very useful function called ' reshape ', which can reshape a Matrix int o A new one with different size and keep its original data.
You ' re given a matrix represented by a two-dimensional array, and both positive integers r and C representing the row number and column number of the wanted reshaped matrix, respectively.
The reshaped matrix need to being filled with all the elements of the original matrix in the same row-traversing< /strong> order as they were.
If the ' reshape ' operation with given parameters are possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
Example 1:
Input: nums = [[], [3,4]]r = 1, c = 4
Output: [[1,2,3,4]]
Explanation: The row-traversing of Nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, and the fill it row by row by using the previous list.
Example 2:
Input: nums = [[], [3,4]]r = 2, c = 4
Output: [[Up], [3,4]]
Explanation: There is no-to-reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
Note:
The height and width of the given matrix is in range [1, 100].
The given R and C is all positive.
Main topic:
Given a two-dimensional matrix nums, it is transformed into a new matrix of row C columns of R. If the conversion cannot be completed, return to the original matrix.
Attention:
The height and width range of a given matrix [1, 100]
Both R and C are positive numbers.
Problem Solving Ideas:
See Code
Code
Reshapematrix.go
Import "FMT" func matrixreshape (Nums [][]int, R int, c int) [][]int {var chanint chan int chanint = make ( Chan int) var length int go func (len *int) {for _, V1: = Range Nums {For _, V: = Range V1 { (*len) + + FMT. Printf ("len1:%+v\n", length) Chanint <-v}} for {chanint <-0 }} (&length) var ret [][]int for I: = 0; I < R; i++ {var lineret []int for J: = 0; J < C; J + + {V: = <-chanint Lineret = Appen D (Lineret, v)} ret = append (ret, Lineret)} fmt. Printf ("len2:%+v\n", length) fmt. Printf ("ret:%+v\n", ret) if R * c! = length {return nums} return ret}