数组降维

数组降维,第1张

数组降维

   地址:566. 重塑矩阵 - 力扣(LeetCode) (leetcode-cn.com)

该题的解法为:以一维数组作为中间商,经一维数组的手把m行n列的数组转为r行c列的数组

降为一维数组的原理为:

 由于j < n 故 i = x / n ; j = x % n;

代码为:

class Solution {
public:
    vector> matrixReshape(vector>& mat, int r, int c) {

        int m = mat.size();
        int n = mat[0].size();
        if(m*n != r*c) return mat;
        
        vector> vet(r,vector(c));
        for(int x = 0 ; x < m*n ; x++){
            vet[x/c][x%c] = mat[x/n][x%n];
        }
        return vet;
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5610748.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存