#33 - Convert 1D Array to 2D Array
You are given a 1D array containing integers and the number of rows m
and number of columns n
of the desired 2D array. Your task is to implement a function to convert the 1D array into a 2D array of size m x n
row-wise.
If the total number of elements in the 1D array is less than m x n
, fill the remaining cells with 0. If the total number of elements is greater than m x n
, ignore the excess elements.
Example 1
Input
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
m = 3
n = 3
Output
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Example 2
Input
nums = [1,2,3,4,5]
m = 2
n = 2
Output
[
[1,2],
[3,4]
]
Example 3
Input
nums = [1,2,3]
m = 2
n = 2
Output
[
[1,2],
[3,0]
]
Last updated