Skip to content

Commit 0caf8c6

Browse files
committed
566. Reshape the Matrix
1 parent b703b13 commit 0caf8c6

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)