#566

Reshape the Matrix

pupil · 345 · lc easy +22 · verified · 64.8% accepted · 3,697 likes · top 68%

play →

Description

Given an m x n matrix mat and target dimensions r and c, rearrange the elements of mat in row-major order into a new r x c matrix. If the total element count differs (i.e., m * n != r * c), return the original matrix unchanged.

Example 1:

Input: mat = [[1,2],[3,4]], r = 1, c = 4
Output: [[1,2,3,4]]

Example 2:

Input: mat = [[1,2],[3,4]], r = 2, c = 4
Output: [[1,2],[3,4]]

Code

1
2
3