题目描述
In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.
You’re given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the ‘reshape’ operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
Example1
|
|
Example2
|
|
Note
- The height and width of the given matrix is in range
[1, 100]
. - The given r and c are all positive.
分析
这道题让我们实现矩阵大小的重塑。
答题思路: 对于这种二维数组大小重新分配的问题的关键就是对应位置的坐标转换,最直接的办法就是把原数组拉直,变成一条直线,然后再组成新数组。
解法1
这道题我们先判断给定数组是否能重塑成给定的大小,就是看两者的元素个数是否相同,直接行数乘以列数即可,然后我们重建一个目标大小的数组,并开始遍历,对于每个位置,我们先转为拉之后的一维坐标,然后在算出在原数组中的对应位置复制过来即可,代码如下:
C++
|
|
Swift
|
|
解法2
下面这种方法整体思路和上面没啥区别,但是只使用了一个循环,直接就是遍历拉直后的一维数组的坐标,然后分别转换为两个二维数组的坐标进行赋值,参见代码如下:
C++
|
|
Swift
|
|