Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int[][] construct2DArray(int[] original, int m, int n) {
- if (original.length != m * n) {
- // Impossible. Return an empty 2D array.
- return new int[0][0];
- }
- int[][] result = new int[m][n];
- for (int i = 0; i < original.length; i++) {
- result[i / n][i % n] = original[i];
- }
- return result;
- }
- }
- class Solution {
- public int[][] construct2DArray(int[] original, int m, int n) {
- if (original.length != m * n) {
- return new int[0][0];
- }
- int[][] arr = new int[m][n];
- int current_index = 0;
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- arr[i][j] = original[current_index];
- current_index++;
- }
- }
- return arr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement