We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b703b13 commit 0caf8c6Copy full SHA for 0caf8c6
Leetcode/July LeetCoding Challenge 2021/566. Reshape the Matrix.js
@@ -0,0 +1,25 @@
1
+/**
2
+ * https://leetcode.com/problems/reshape-the-matrix/
3
+ * @param {number[][]} mat
4
+ * @param {number} r
5
+ * @param {number} c
6
+ * @return {number[][]}
7
+ */
8
+var matrixReshape = function(mat, r, c) {
9
+ if (mat.length * mat[0].length !== r * c) return mat
10
+ const ans = Array(r).fill(0).map(() => [])
11
+ let k = 0
12
+ let l = 0
13
+ for (let i = 0; i < mat.length; i++) {
14
+ for (let j = 0; j < mat[0].length; j++) {
15
+ if (l === c) {
16
+ l = 0
17
+ k++
18
+
19
+ }
20
+ ans[k].push(mat[i][j])
21
+ l++
22
23
24
+ return ans
25
+};
0 commit comments