566. 重塑矩阵

566. 重塑矩阵

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

        if(r*c > m*n)
        {
            return nums;
        }

        int nR = 0;
        int nC = 0;
        for (int i = 0; i < m; ++i) 
        {
            for (int j = 0; j < n; ++j) 
            {
                ans[nR][nC] = nums[i][j];
                nC++;
                if (nC == c) 
                {
                    nR += 1;
                    nC = 0;
                }
            }
        }
        return ans;
    }
};
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;
    }
};

 

上一篇:BUUCTF(1):test_your_nc


下一篇:SQL手工注入