2022. Convert 1D Array Into 2D Array Easy
1/**
2 * [2022] Convert 1D Array Into 2D Array
3 *
4 * You are given a 0-indexed 1-dimensional (1D) integer array original, and two integers, m and n. You are tasked with creating a 2-dimensional (2D) array with m rows and n columns using all the elements from original.
5 * The elements from indices 0 to n - 1 (inclusive) of original should form the first row of the constructed 2D array, the elements from indices n to 2 * n - 1 (inclusive) should form the second row of the constructed 2D array, and so on.
6 * Return an m x n 2D array constructed according to the above procedure, or an empty 2D array if it is impossible.
7 *
8 * Example 1:
9 * <img src="https://assets.leetcode.com/uploads/2021/08/26/image-20210826114243-1.png" style="width: 500px; height: 174px;" />
10 * Input: original = [1,2,3,4], m = 2, n = 2
11 * Output: [[1,2],[3,4]]
12 * Explanation: The constructed 2D array should contain 2 rows and 2 columns.
13 * The first group of n=2 elements in original, [1,2], becomes the first row in the constructed 2D array.
14 * The second group of n=2 elements in original, [3,4], becomes the second row in the constructed 2D array.
15 *
16 * Example 2:
17 *
18 * Input: original = [1,2,3], m = 1, n = 3
19 * Output: [[1,2,3]]
20 * Explanation: The constructed 2D array should contain 1 row and 3 columns.
21 * Put all three elements in original into the first row of the constructed 2D array.
22 *
23 * Example 3:
24 *
25 * Input: original = [1,2], m = 1, n = 1
26 * Output: []
27 * Explanation: There are 2 elements in original.
28 * It is impossible to fit 2 elements in a 1x1 2D array, so return an empty 2D array.
29 *
30 *
31 * Constraints:
32 *
33 * 1 <= original.length <= 5 * 10^4
34 * 1 <= original[i] <= 10^5
35 * 1 <= m, n <= 4 * 10^4
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/convert-1d-array-into-2d-array/
41// discuss: https://leetcode.com/problems/convert-1d-array-into-2d-array/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46 pub fn construct2_d_array(original: Vec<i32>, m: i32, n: i32) -> Vec<Vec<i32>> {
47 vec![]
48 }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_2022() {
59 }
60}
61
Back
© 2025 bowen.ge All Rights Reserved.