[C++] [Java] Clean Code - 5 lines (2 Solution)
Java\n\nclass Solution {\n    public int[][] matrixReshape(int[][] nums, int r, int c) {\n        int m = nums.length, n = nums[0].length, o = m * n;\n        i
**Java**\n```\nclass Solution {\n    public int[][] matrixReshape(int[][] nums, int r, int c) {\n        int m = nums.length, n = nums[0].length, o = m * n;\n        if (r * c != o) return nums;\n        int[][] res = new int[r][c];\n        for (int i = 0; i < o; i++) res[i / c][i % c] = nums[i / n][i % n];\n        return res;        \n    }\n}\n```\n**C++**\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {\n        int m = nums.size(), n = nums[0].size(), o = m * n;\n        if (r * c != o) return nums;\n        vector<vector<int>> res(r, vector<int>(c, 0));\n        for (int i = 0; i < o; i++) res[i / c][i % c] = nums[i / n][i % n];\n        return res;\n    }\n};\n```\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {\n        int m = nums.size(), n = nums[0].size();\n        if (m * n != r * c) {\n            return nums;\n        }\n\n        vector<vector<int>> res(r, vector<int>(c, 0));\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                int k = i * n + j;\n                res[k / c][k % c] = nums[i][j];\n            }\n        }\n\n        return res;\n    }\n};\n```
[]