Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9e5a6bf

Browse files
committed
Add solution #2022
1 parent 3811e5e commit 9e5a6bf

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,691 LeetCode solutions in JavaScript
1+
# 1,692 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1550,6 +1550,7 @@
15501550
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
15511551
2018|[Check if Word Can Be Placed In Crossword](./solutions/2018-check-if-word-can-be-placed-in-crossword.js)|Medium|
15521552
2019|[The Score of Students Solving Math Expression](./solutions/2019-the-score-of-students-solving-math-expression.js)|Hard|
1553+
2022|[Convert 1D Array Into 2D Array](./solutions/2022-convert-1d-array-into-2d-array.js)|Easy|
15531554
2027|[Minimum Moves to Convert String](./solutions/2027-minimum-moves-to-convert-string.js)|Easy|
15541555
2033|[Minimum Operations to Make a Uni-Value Grid](./solutions/2033-minimum-operations-to-make-a-uni-value-grid.js)|Medium|
15551556
2037|[Minimum Number of Moves to Seat Everyone](./solutions/2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 2022. Convert 1D Array Into 2D Array
3+
* https://leetcode.com/problems/convert-1d-array-into-2d-array/
4+
* Difficulty: Easy
5+
*
6+
* You are given a 0-indexed 1-dimensional (1D) integer array original, and two integers,
7+
* m and n. You are tasked with creating a 2-dimensional (2D) array with m rows and n
8+
* columns using all the elements from original.
9+
*
10+
* The elements from indices 0 to n - 1 (inclusive) of original should form the first row
11+
* of the constructed 2D array, the elements from indices n to 2 * n - 1 (inclusive) should
12+
* form the second row of the constructed 2D array, and so on.
13+
*
14+
* Return an m x n 2D array constructed according to the above procedure, or an empty 2D array
15+
* if it is impossible.
16+
*/
17+
18+
/**
19+
* @param {number[]} original
20+
* @param {number} m
21+
* @param {number} n
22+
* @return {number[][]}
23+
*/
24+
var construct2DArray = function(original, m, n) {
25+
if (original.length !== m * n) return [];
26+
27+
const result = [];
28+
for (let i = 0; i < m; i++) {
29+
result.push(original.slice(i * n, (i + 1) * n));
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)