C++ Solution


  • 0
    Q
    class Solution {
    public:
        vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
            if( !nums.size() || r * c != nums.size() * nums[0].size() )return nums;
            vector<vector<int> > res(r,vector<int>(c,0));
            int b = nums[0].size();
            int len = nums.size() * b;
            int index_r = 0,index_c = 0;
            for(int i = 0 ; i < len ; ++i){
                res[i/c][i%c] = nums[i/b][i%b];
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.