C++ Two Simple and Easy Solutions (6-Lines)
First Solution - Intuitive:\nJust loop with nested loop through the original matrix, and keep track of the row and col in the new matrix.\n\nclass Solution {\np
**First Solution - Intuitive:\nJust loop with nested loop through the original matrix, and keep track of the row and col in the new matrix.**\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n        int area = mat.size() * mat[0].size();\n        if (area != r * c) return mat;\n        \n        vector<vector<int>> res(r, vector<int>(c));\n        int row = 0, col = 0;\n        \n        for (int i = 0; i < mat.size(); i++) {\n            for (int j = 0; j < mat[i].size(); j++) {\n                \n                res[row][col] = mat[i][j];\n                col++;\n                \n                if (col == c) {\n                    col = 0; \n                    row++;\n                }\n            }\n        }\n        \n        return res;\n    }\n};\n```\n**Second Solution - Shorter:\nSame idea, just with only one loop, using the same iterator to figure out where we are up to in both matrices.**\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n        int area = mat.size() * mat[0].size();\n        if (area != r * c) return mat;\n        \n        vector<vector<int>> res(r, vector<int>(c));\n        \n        for (int i = 0; i < area; i++) {\n            res[i/c][i%c] = mat[i/mat[0].size()][i%mat[0].size()];\n        }\n\n        return res;\n    }\n};\n```\n**Like it? please upvote!**
['C']