[LeetCode]566. Reshape the Matrix(重塑矩阵)

566. Reshape the Matrix

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.

题目大意:
重塑一个矩阵,需要给出新矩阵的行和列,若新矩阵的行和列不符合规则(新矩阵元素个数不等于原矩阵)就返回原矩阵,否则就返回新矩阵。不用排序

重塑矩阵规则:
原矩阵从上到下,从左到右取出元素,依次赋值给新矩阵对应位置(从上到下,从左到右)

思路:
设置一个不定长一维数组num存放原矩阵的所有数据,按要求设置新矩阵NUMS并依次赋值(对应num中相应位置)

代码如下:

#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        if(nums[0].size()*nums.size() != r*c)
            return nums;
        vector<int> num;//顺序放置原矩阵的元素
        for(int i=0; i<nums.size(); i++)
            for(int j=0; j<nums[0].size(); j++)
                num.push_back(nums[i][j]);
        vector<vector<int>> NUMS(r, vector<int>(c, 0));
        for(int i=0; i<r; i++)
            for(int j=0; j<c; j++)
                NUMS[i][j] = num[i*c+j];
        return NUMS;
    }
    vector<vector<int>> matrixReshape1(vector<vector<int>>& nums, int r, int c) {
        int R = nums.size();//原矩阵行
        int C = nums[0].size();//原矩阵列
        if(R*C != r*c)
            return nums;
        vector<vector<int>> NUMS(r);//构造新矩阵 设置行为r
        for (int i = 0; i < NUMS.size(); i++)//设置新矩阵每行的列数为c
            NUMS[i].resize(c);
        for(int i=0; i<r; i++)
            for(int j=0; j<c; j++){
                int a = (i*c+j)/C;//对应原矩阵的行
                int b = (i*c+j)%C;//对应原矩阵的列
                NUMS[i][j] = nums[a][b];
            }
        return NUMS;
    }
};
int main()
{
    Solution a;
    int R,C,r,c;//原矩阵行和列
    cout <<"输入矩阵的行: ";
    cin >> R;
    cout <<"输入矩阵的列: ";
    cin >> C;
    cout << "依次输入矩阵元素: " << endl;
    vector<vector<int>> nums(R, vector<int>(C, 0));//矩阵默认值为0
    for (int i = 0; i <R; i++){
        for (int j = 0; j <C; j++)
            cin >> nums[i][j];
        cout << endl;
    }
    cout << "输出矩阵: " << endl;
    for (int i = 0; i <R; i++){
        for (int j = 0; j <C; j++)
            cout << nums[i][j];
        cout << endl;
    }
    cout <<"输入新矩阵的行: ";
    cin >> r;
    cout <<"输入新矩阵的列: ";
    cin >> c;
    cout << "输出新矩阵: " << endl;
    vector <vector<int>>  NUMS = a.matrixReshape1(nums,r,c);
    for (int i = 0; i <r; i++){
        for (int j = 0; j <c; j++)
            cout << NUMS[i][j];
        cout << endl;
    }
    return 0;
}

注意:

  • 第一种解决方案时间复杂度低,但是空间复杂度高,第二种和第一种相反
  • 做完后看了一下discuss,看到一个java的解决方案,用得很巧妙,特别是最后的K值应用,点击查看
  • 自己的代码中掺杂了两种不定长二维数组表示方法,可以参考
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值