地址: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; } };
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)