Skip to content

Commit 471fd13

Browse files
committedDec 24, 2021
solves convert 1d array to 2d array
1 parent bab6edb commit 471fd13

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@
482482
| 2006 | [Count Number of Pairs With Absolute Difference K](https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k) | [![Java](assets/java.png)](src/CountNumberOfPairsWithAbsoluteDifferenceK.java) | |
483483
| 2011 | [Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations) | [![Java](assets/java.png)](src/FinalValueOfVariableAfterPerformingOperations.java) | |
484484
| 2016 | [Maximum Difference Between Increasing Elements](https://leetcode.com/problems/maximum-difference-between-increasing-elements) | [![Java](assets/java.png)](src/MaximumDifferenceBetweenIncreasingElements.java) | |
485-
| 2022 | [Convert 1D Array Into 2D Array](https://leetcode.com/problems/convert-1d-array-into-2d-array) | | |
485+
| 2022 | [Convert 1D Array Into 2D Array](https://leetcode.com/problems/convert-1d-array-into-2d-array) | [![Java](assets/java.png)](src/Convert1DArrayInto2DArray.java) | |
486486
| 2027 | [Minimum Moves to Convert String](https://leetcode.com/problems/minimum-moves-to-convert-string) | | |
487487
| 2032 | [Two Out of Three](https://leetcode.com/problems/two-out-of-three) | | |
488488
| 2037 | [Minimum Number of Moves to Seat Everyone](https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone) | | |

‎src/Convert1DArrayInto2DArray.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// https://leetcode.com/problems/convert-1d-array-into-2d-array
2+
// T: O(n * m)
3+
// S: O(n * m)
4+
5+
public class Convert1DArrayInto2DArray {
6+
public int[][] construct2DArray(int[] original, int m, int n) {
7+
if (m * n != original.length) return new int[0][];
8+
final int[][] result = new int[m][n];
9+
for (int i = 0 ; i < m ; i++) {
10+
for (int j = 0 ; j < n ; j++) {
11+
result[i][j] = original[n * i + j];
12+
}
13+
}
14+
return result;
15+
}
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.