Leetcode 566.マトリックスの形状を変更します(Matlabの形状変更の簡単な実現)

 

この質問は比較的単純で、最初に2次元配列を1次元表現に変換し、次に別の2次元表現に変換します。実際、1次元表現に変換するプロセスは省略できます。

class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int m = nums.size();
        int n = nums[0].size();
        if (m * n != r * c) {
            return nums;
        }

        vector<vector<int>> ans(r, vector<int>(c));
        for (int x = 0; x < m * n; ++x) {
            ans[x / c][x % c] = nums[x / n][x % n];
        }
        return ans;
    }
};

 

おすすめ

転載: blog.csdn.net/wwxy1995/article/details/113830070