首页 > 编程语言 > 详细

leetcode566 C++ 36ms 矩阵reshape

时间:2018-08-04 23:12:19      阅读:214      评论:0      收藏:0      [点我收藏+]
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;
    }
};

leetcode566 C++ 36ms 矩阵reshape

原文:https://www.cnblogs.com/theodoric008/p/9420348.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!