java: add «2022. Convert 1D Array Into 2D Array»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
f848a7b9c7
commit
4e73b65849
1 changed files with 15 additions and 0 deletions
15
java/convert-1d-array-into-2d-array.java
Normal file
15
java/convert-1d-array-into-2d-array.java
Normal file
|
@ -0,0 +1,15 @@
|
|||
class Solution {
|
||||
public int[][] construct2DArray(int[] original, int m, int n) {
|
||||
if (original.length != m * n) {
|
||||
return new int[0][0];
|
||||
}
|
||||
|
||||
var array = new int[m][n];
|
||||
for (int i = 0; i < m * n; ++i) {
|
||||
int y = i / n, x = i % n;
|
||||
array[y][x] = original[i];
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue