C++ || EASY TO UNDERSTAND || FAST || 3 methods
Please upvote to motivate me in my quest of documenting all leetcode solutions(to help the community). HAPPY CODING:)\nAny suggestions and improvements are alwa
**Please upvote to motivate me in my quest of documenting all leetcode solutions(to help the community). HAPPY CODING:)\nAny suggestions and improvements are always welcome**\n\n**1st method**\n```\nclass Solution{\n    public:\n     vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n     int n=mat.size(),m=mat[0].size();\n         int all=n*m;\n         if(all!=r*c)\n          return mat;   \n     vector<vector<int>> ans(r,vector<int>(c,0));\n        for(int i=0;i<all;i++)\n        {\n            ans[i/c][i%c]=mat[i/m][i%m];\n        }\n        return ans; \n     }\n};\n```\n**2nd method**\n```\nclass Solution {\npublic:\n    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n     int n=mat.size(),m=mat[0].size(),k=0;\n    if(c*r!=n*m)\n        return mat;\n        vector<vector<int>> ans;\n        vector<int> v;\n     for(int i=0;i<n;i++)\n        {\n         for(int j=0;j<m;j++)\n         {\n         v.push_back(mat[i][j]);\n             k++;\n             if(k==c)\n             {\n                k=0; \n                ans.push_back(v); \n                v.clear();    \n             }\n        }\n     }\n        return ans;\n    }\n};\n```\n**Likewise,**\n```\nclass Solution{\n    public:\n     vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {\n      int n=mat.size(),m=mat[0].size();\n         if(m*n!=r*c)\n          return mat;\n          vector<vector<int>> ans(r, vector<int>(c, 0));\n         for(int i=0;i<n;i++)\n         {\n             for(int j=0;j<m;j++)\n             {\n                int k=m*i+j;\n                 ans[k/c][k%c]=mat[i][j];\n             }\n         }\n         return ans;\n     }\n};\n```
['C']