From 4e73b6584968f720eb63b2d10523b90b0cf074cf Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 1 Sep 2024 13:53:04 +0200 Subject: [PATCH] =?UTF-8?q?java:=20add=20=C2=AB2022.=20Convert=201D=20Arra?= =?UTF-8?q?y=20Into=202D=20Array=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- java/convert-1d-array-into-2d-array.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 java/convert-1d-array-into-2d-array.java diff --git a/java/convert-1d-array-into-2d-array.java b/java/convert-1d-array-into-2d-array.java new file mode 100644 index 0000000..00b2eb0 --- /dev/null +++ b/java/convert-1d-array-into-2d-array.java @@ -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; + } +}