Skip to content

Commit 2b51bee

Browse files
authored
Create Convert 1D Array Into 2D Array.java
1 parent 1eec20b commit 2b51bee

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int[][] construct2DArray(int[] original, int m, int n) {
3+
if (m * n != original.length) {
4+
return new int[][]{};
5+
}
6+
int[][] arr = new int[m][n];
7+
int idx = 0;
8+
int arrIdx = 0;
9+
while (idx < original.length) {
10+
for (int i = 0; i < n; i++) {
11+
arr[arrIdx][i] = original[idx++];
12+
}
13+
arrIdx++;
14+
}
15+
return arr;
16+
}
17+
}

0 commit comments

Comments
 (0)