C++ solutions


  • 0
    R
    class Solution {
    public:
        vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        if(nums[0].size()*nums.size()!=r*c)
            return nums;
        vector<vector<int>> result(r,vector<int>(c));
        int n=nums[0].size();
        for(int i=0; i<c*r;i++)
            result[i/c][i%c]=nums[i/n][i%n];
        return result;
        }
    };
    

Log in to reply
 

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