do question 3
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MainExercises
|
||||
{
|
||||
@@ -150,11 +151,67 @@ public class MainExercises
|
||||
body to use them instead of arrays.
|
||||
*/
|
||||
|
||||
private static List<List<Integer>> result;
|
||||
private static List<Integer> help;
|
||||
|
||||
|
||||
public int[][] intPartitions(int n) {
|
||||
// todo
|
||||
return null;
|
||||
result = new ArrayList<>();
|
||||
help = new ArrayList<>();
|
||||
|
||||
findRecursive(n, n);
|
||||
|
||||
int[][] answer = casting(result);
|
||||
|
||||
return answer;
|
||||
}
|
||||
|
||||
private static void findRecursive(int target , int max){
|
||||
|
||||
if(target == 0){
|
||||
|
||||
result.add(new ArrayList<>(help));
|
||||
return;
|
||||
}
|
||||
|
||||
for(int i = Math.min(target , max) ; i >= 1 ; i--){
|
||||
help.add(i);
|
||||
|
||||
|
||||
findRecursive(target -i , i);
|
||||
|
||||
help.remove(help.size() -1);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private int[][] casting(List<List<Integer>> input){
|
||||
int n = input.size();
|
||||
|
||||
int[][] answer = new int[n][];
|
||||
|
||||
for(int i = 0 ; i < n ; i++){
|
||||
int k = input.get(i).size();
|
||||
answer[i] = new int[k];
|
||||
|
||||
for(int j = 0 ; j < k ; j++){
|
||||
answer[i][j] = input.get(i).get(j);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return answer;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user