s4553711
12/20/2017 - 2:42 PM

566.cpp

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();
        int n = o_c * o_r;
        if (n == r*c) {
            vector<vector<int>> results(r, vector<int>(c, 0));
            for(int i = 0; i < n; i++) {
                results[i / c][i % c] = nums[i / o_c][i % o_c];
            }
            return results;
            
        } else {
            return nums;
        }
    }
};