Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a32205a

Browse files
566. Reshape the Matrix
1 parent 4cf5650 commit a32205a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎src/566. Reshape the Matrix.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 566. Reshape the Matrix
3+
* https://leetcode.com/problems/reshape-the-matrix/
4+
* @param {number[][]} nums
5+
* @param {number} r
6+
* @param {number} c
7+
* @return {number[][]}
8+
*/
9+
var matrixReshape = function (nums, r, c) {
10+
if (r * c !== nums.length * nums[0].length) {
11+
return nums;
12+
}
13+
let res = Array(r);
14+
let index = 0;
15+
for (let i = 0; i < r; i++) {
16+
res[i] = Array(c).fill(0);
17+
for (let j = 0; j < c; j++) {
18+
index = i * c + j;
19+
res[i][j] = nums[Math.trunc(index / nums[0].length)][index % nums[0].length]
20+
}
21+
}
22+
return res;
23+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /