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;
    }
};
posted @ 2018-08-04 22:05  一条图图犬  阅读(265)  评论(0编辑  收藏  举报