C++ clear way don`t need explain beat 90%


  • 0
    C

    class Solution {
    public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
    int row = nums.size();
    int col = nums[0].size();
    if(row * col != r*c)
    return nums;
    vector<int> temp;
    for(int i=0;i<row; i++){
    for(int j=0;j<col;j++){
    temp.push_back(nums[i][j]);
    }
    }
    vector<vector<int>>ans;
    int curr=0;
    for(int i=0;i<r; i++){
    ans.push_back(vector<int>());
    for(int j=0;j<c;j++){
    ans[i].push_back(temp[curr]);
    curr++;
    }
    }
    return ans;
    }
    };


Log in to reply
 

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