566. Reshape the Matrix。

来源:互联网 发布:汉诺塔递归算法图解 编辑:程序博客网 时间:2024/06/05 00:36

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.

Example 1:

Input:
nums =
[[1,2],
[3,4]]
r = 1, c = 4
Output:
[[1,2,3,4]]
Explanation:
The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

Example 2:

Input:
nums =
[[1,2],
[3,4]]
r = 2, c = 4
Output:
[[1,2],
[3,4]]
Explanation:
There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.

Note:
1.The height and width of the given matrix is in range [1, 100].
2.The given r and c are all positive.


题中的意思就是将一个二维数组重新改变形状,也就是说为的reshape函数(在Python中的pandas也存在,MATLAB也存在),可以对一个二维数组重新指定形状。题中的第一个例子就是将一个2*2的数组转换为1*4的形状,其中的元素就是按照原来的顺序依次排放。第二个例子说明了如果原先的数组不能转变成指定的形状,就返回原来的数组。

刚开始想的就是,将原来的二维数组中的内容全部取出来存放在队列中,然后创建一个指定形状(行数为r,列数为c)的二维数组,依次将队列中的内容取出来进行赋值即可。

class Solution {public:    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {        if(nums.size() == 0 || nums.size() * nums[0].size() != r*c) {//如果等于0或者不能转换为指定的要求则返回原数组            return nums;        }        queue<int> all_nums;        vector<vector<int>> new_nums;        int i,j;        new_nums.resize(r);//设定为r行        for(i=0;i<r;i++) {            new_nums[i].resize(c);//设定每列多少个元素        }        for(i=0;i<nums.size();i++) {//将所有的数字放到一个队列中            for(j=0;j<nums[i].size();j++) {                //cout << nums[i][j] << endl;                all_nums.push(nums[i][j]);            }        }        for(i=0;i<r;i++) {//新的行数            for(j=0;j<c;j++) {//新的列数                new_nums[i][j] = all_nums.front();                all_nums.pop();            }        }        return new_nums;    }};

上面的方法需要使用额外的空间来存储原来的内容,其实在我们创建完制定的二维数组之后就可以进行存放了,我们可以使用两个变量:row和col,分别用来标记需要将元素存放在二维数组中的什么位置,row和col是从0开始的,然后col进行加一并且判断此时的col和要求的c是不是一样,如果一样的话说明这一行已经存满了,将row进行加一代表着开始存放第二行数据,并且col需要置0.

例如,将1*4(1,2,3,4)的转变为2*2的:

创建一个2*2的数组,然后row和col都为0,

所以第一次赋值:new_nums[0][0] = 1,然后col加一并且判断此刻col是不是等于2(也就是题中的2),此刻不等于。

第二次赋值:new_nums[0][1] = 2,col加一发现此刻col等于2,所以说明这一行存满了,需要下一行了。所以row加一,col置0.

第三次赋值:new_nums[1][0] = 3,col加一,此刻不等于2。

第四次赋值:new_nums[1][1] = 4,col加一,row加一,但是原数组中已经没有内容了,就结束了。产生的new_nums就是我们需要的形状。

class Solution {public:    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {        if(nums.size() == 0 || nums.size() * nums[0].size() != r*c) {//如果等于0或者不能转换为指定的要求则返回原数组            return nums;        }        vector<vector<int>> new_nums(r,vector<int>(c));        int i,j;        int row=0,col=0;        for(i=0;i<nums.size();i++) {//处理行            for(j=0;j<nums[0].size();j++) {//处理列                new_nums[row][col] = nums[i][j];//取出一个元素赋值给新的数组                col++;//列加一,向后移动一个                if(col == c) {//如果此刻的列数已经增加到了规定的列数,就让行数加一,列数清零                    col = 0;                    row++;                }            }        }        return new_nums;    }};