C++ easy to understand beats 90.72%


  • 0
        vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
            if(nums.size()*nums[0].size()!=r*c) return nums;
            vector<vector<int>>res(r,vector<int>(c));
            int row=0;
            int col=0;
            for(int i=0;i<r;i++){
                for(int j=0;j<c;j++){
                    res[i][j]=nums[row][col];
                    col++;
                    if(col==nums[0].size()) col=0,row++;
                }
            }
            return res;
        }
    

Log in to reply
 

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