Reshape Matrix

MATRIX

Problem

Given a matrix mat of size m x n and two integers r and c, reshape the matrix into a new one with r rows and c columns. If the reshaping operation cannot be done, return the original matrix.

You should fill the new matrix row by row with the elements from the original matrix in the same row-traversing order.

Examples

matrixReshape([[5,6],[7,8]], 1, 4) // returns [[5,6,7,8]] // The original matrix is reshaped to a 1x4 matrix. // The elements are filled in row-traversing order. matrixReshape([[5,6],[7,8]], 3, 2) // returns [[5,6],[7,8]] // It's impossible to reshape the matrix to a 3x2 matrix // so the original matrix is returned. matrixReshape([[9,10],[11,12]], 2, 2) // returns [[9,10],[11,12]] // The request does not change the dimensions // so the original matrix is returned.
Loading...
5
6
7
8