File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 62
62
459|[ Repeated Substring Pattern] ( ./0459-repeated-substring-pattern.js ) |Easy|
63
63
541|[ Reverse String II] ( ./0541-reverse-string-ii.js ) |Easy|
64
64
565|[ Array Nesting] ( ./0565-array-nesting.js ) |Medium|
65
+ 566|[ Reshape the Matrix] ( ./0566-reshape-the-matrix.js ) |Easy|
65
66
606|[ Construct String from Binary Tree] ( ./0606-construct-string-from-binary-tree.js ) |Easy|
66
67
617|[ Merge Two Binary Trees] ( ./0617-merge-two-binary-trees.js ) |Easy|
67
68
628|[ Maximum Product of Three Numbers] ( ./0628-maximum-product-of-three-numbers.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 566. Reshape the Matrix
3
+ * https://leetcode.com/problems/reshape-the-matrix/
4
+ * Difficulty: Easy
5
+ *
6
+ * In MATLAB, there is a handy function called reshape which can reshape an m x n
7
+ * matrix into a new one with a different size r x c keeping its original data.
8
+ *
9
+ * You are given an m x n matrix mat and two integers r and c representing the
10
+ * number of rows and the number of columns of the wanted reshaped matrix.
11
+ *
12
+ * The reshaped matrix should be filled with all the elements of the original
13
+ * matrix in the same row-traversing order as they were.
14
+ *
15
+ * If the reshape operation with given parameters is possible and legal, output
16
+ * the new reshaped matrix; Otherwise, output the original matrix.
17
+ */
18
+
19
+ /**
20
+ * @param {number[][] } mat
21
+ * @param {number } r
22
+ * @param {number } c
23
+ * @return {number[][] }
24
+ */
25
+ var matrixReshape = function ( mat , r , c ) {
26
+ const flat = mat . flat ( ) ;
27
+ const result = [ ] ;
28
+
29
+ if ( flat . length !== r * c ) {
30
+ return mat ;
31
+ }
32
+
33
+ while ( flat . length ) {
34
+ result . push ( flat . splice ( 0 , c ) ) ;
35
+ }
36
+
37
+ return result ;
38
+ } ;
You can’t perform that action at this time.
0 commit comments