leetcode566 C++ 36ms 矩阵reshape

class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        if(nums.empty() || nums[0].empty()){
            return {};
        }
        auto h = nums.size();
        auto w = nums[0].size();
        if(h*w != r*c){
            return nums;
        }
        vector<vector<int>> res(r, vector<int>(c, 0));
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res[i][j] = nums[(i*c + j)/w][(i*c + j)%w];
            }
        }
        return res;
    }
};

猜你喜欢

转载自www.cnblogs.com/theodoric008/p/9420348.html