📘
Reshape the Matrix
Given an m x n matrix, reshape it to r x c. If not possible, return the original matrix.
Output each row on a line, space-separated.
Example:
Input:
1,2 3,4 1 4
Output:
1 2 3 4
Reasoning:
- The input matrix is 1×2, with values
1,2and3,4, but the actual matrix is defined by the first line as a 1×2 matrix with values1and2, and the second line as a 1×2 matrix with values3and4. - The target reshape dimensions are given as
1 4, meaning 1 row and 4 columns, but since the total number of elements in the original matrix is 4 (2 elements per row × 2 rows), we can reshape it to 1×4. - We then flatten the original matrix into a single array:
[1, 2, 3, 4]. - The final output is this flattened array reshaped into 1×4 dimensions:
1 2 3 4
Constraints:
- 1 <= m, n <= 100
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.