566. Reshape the Matrix Flashcards

1
Q

Can you set up variables for mat.length and mat[0].length?

A

let m = mat.length, n = mat[0].length;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can you set up the restrict condition for return the original mat?

A

let m = mat.length, n = mat[0].length;
if (m * n !== r * c) return mat;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can you set up variable res for empty two dimensional arrays and stack for empty array?

A

let m = mat.length, n = mat[0].length;
if (m * n !== r * c) return mat;
let res = new Array(r).fill(0).map(() => new Array(c).fill(0)), stack = [];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can you loop through mat and push each element to the empty stack?

A

let m = mat.length, n = mat[0].length;
if (m * n !== r * c) return mat;
let res = new Array(r).fill(0).map(() => new Array(c).fill(0)), stack = [];
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
stack.push(mat[i][j]);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you loop through from 0 to kth elements in stack and assign it to the empty two dimensional array res?

A

let m = mat.length, n = mat[0].length;
if (m * n !== r * c) return mat;
let res = new Array(r).fill(0).map(() => new Array(c).fill(0)), stack = [];
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
stack.push(mat[i][j]);
}
}
let k = 0;
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j++) {
res[i][j] = stack[k];
k++;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you return the two dimensional array?

A

return res;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly