✅[C#][Go] Detailed Explanation✅
Get the number of rows n and columns m of the input matrix mat.\nCheck if n * m is equal to the number of elements in the new matrix, i.e., r * c. If they are n
Get the number of rows n and columns m of the input matrix mat.\nCheck if `n * m` is equal to the number of elements in the new matrix, i.e., `r * c`. If they are not equal, return the original matrix mat.\nCreate a new matrix arr of dimensions `r x c` using a nested for loop.\nIterate over the elements of mat using a nested for loop and fill in the elements of arr in row-major order. To do this, use the formula `l = i * m + j` to calculate the linear index of each element in mat, and the formula `arr[l / c][l % c] = mat[i][j]` to assign the element to the appropriate position in arr.\nReturn the reshaped matrix arr.\n\n# Complexity\n- Time complexity: $$O(r * c)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(r * c)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n##### Go\n```\nfunc matrixReshape(mat [][]int, r int, c int) [][]int {\n\tn := len(mat)\n\tm := len(mat[0])\n\tif m * n != r * c {\n\t\treturn mat\n\t}\n\tarr := make([][]int, r)\n\tfor i := 0; i < r; i++ {\n\t\tarr[i] = make([]int, c)\n\t}\n\tfor i := 0; i < n; i++ {\n\t\tfor j := 0; j < m; j++ {\n\t\t\tl := i*m + j\n\t\t\tarr[l/c][l%c] = mat[i][j]\n\t\t}\n\t}\n\n\treturn arr\n}\n```\n##### C#\n```\npublic class Solution {\n\tpublic int[][] MatrixReshape(int[][] mat, int r, int c) {\n\t\tint n = mat.Length;\n\t\tint m = mat[0].Length;\n\t\t\n\t\tif (m * n != r * c) {\n\t\t\treturn mat;\n\t\t}\n\t\t\n\t\tint[][] arr = new int[r][];\n\t\tfor (int i = 0; i < r; i++) {\n\t\t\tarr[i] = new int[c];\n\t\t}\n\t\t\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tfor (int j = 0; j < m; j++) {\n\t\t\t\tint l = i * m + j;\n\t\t\t\tarr[l / c][l % c] = mat[i][j];\n\t\t\t}\n\t\t}\n\t\t\n\t\treturn arr;\n\t}\n}\n```\n![a6c83c54-1d1a-4f26-8273-b687d119dd5b_1679889261.1494205.png](https://assets.leetcode.com/users/images/cc82e8aa-dc98-4f10-af4c-64b1528c0fb6_1680844232.130332.png)
['Array', 'Math', 'Matrix', 'Go', 'C#']