1170. Reshape the matrix
Chinese
English
In MATLAB, there is a very useful function called 'reshape', which can reshape the matrix into a matrix of different size, but retain its original data.
First give a matrix represented by a two-dimensional array, and two positive integers r and c, respectively representing the number of rows and columns of the required reshaping matrix.
The reformed matrix needs to be filled with all the elements of the original matrix in the same row traversal order.
If the "reshape" operation using the given parameters is possible and legal, a new reshaping matrix is output; otherwise, the original matrix is output.
Sample
Example 1:
输入:
nums =
[[1,2],
[3,4]]
r = 1, c = 4
输出:
[[1,2,3,4]]
解释:
行遍历的顺序为 [1,2,3,4]. 新给出的矩阵大小为1 * 4, 根据前面给出的列表按行遍历即可。
Example 2:
输入:
nums =
[[1,2],
[3,4]]
r = 2, c = 4
输出:
[[1,2],
[3,4]]
解释:
无法将一个2 * 2的矩阵整形称为一个2 * 4的矩阵。 所以输出原始矩阵。
Precautions
The size range of matrix rows and columns is [1, 100].
Both r and c are positive numbers.