HW 02 (2)
This commit is contained in:
@@ -153,10 +153,25 @@ public class MainExercises
|
|||||||
body to use them instead of arrays.
|
body to use them instead of arrays.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static ArrayList<ArrayList<Integer>> intPartitions(int n) {
|
public static int[][] intPartitions(int n) {
|
||||||
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
|
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
|
||||||
backtrack(n, n, new ArrayList<>(), result);
|
backtrack(n, n, new ArrayList<>(), result);
|
||||||
return result;
|
|
||||||
|
ArrayList<int[]> resLst = new ArrayList<int[]>();
|
||||||
|
for (int i = 0; i < result.size(); i++) {
|
||||||
|
int[] row = new int[result.get(i).size()];
|
||||||
|
for (int j = 0; j < result.get(i).size(); j++) {
|
||||||
|
row[j] = result.get(i).get(j);
|
||||||
|
}
|
||||||
|
resLst.add(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
int[][] res = new int[resLst.size()][];
|
||||||
|
for (int k = 0; k < resLst.size(); k++) {
|
||||||
|
res[k] = resLst.get(k);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void backtrack(int remaining, int max,
|
private static void backtrack(int remaining, int max,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
|
||||||
|
|
||||||
public class MainTestPartitions {
|
public class MainTestPartitions {
|
||||||
static MainExercises ex;
|
static MainExercises ex;
|
||||||
|
|
||||||
@@ -63,3 +64,5 @@ public class MainTestPartitions {
|
|||||||
assertArrayEquals(expected, ex.intPartitions(6));
|
assertArrayEquals(expected, ex.intPartitions(6));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user