C++ solution


  • 0
    L
    class Solution {
    public:
        vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
            int o_r = nums.size();
            int o_c = nums[0].size();
            vector<vector<int>> ret;
            if ((o_r*o_c)!=(r*c)) return nums;
            
            int row_c = 0;
            int col_c = 0;
            vector<int> row;
            for (int o_index_r = 0 ; o_index_r < o_r; ++ o_index_r){
                for (int o_index_c = 0 ; o_index_c < o_c; ++ o_index_c){
                    row.push_back(nums[o_index_r][o_index_c]); ++col_c;
                    if (col_c == c){
                      ret.push_back(row);
                      col_c= 0;
                      ++row_c;
                      row.clear();
                    } 
                }
            }
            return ret;
        }
    };
    

Log in to reply
 

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