I am looking to generate more permutation that sums up to a given number N
, however this time more efficiently. Since by general methods, it takes forever to create 100+ permutations.
However I'm at another impasses where I find it extremely difficult to build upward permutations that utilized the permutations that are already solved n-1
to generate every permutation that sums to n
.
I would greatly appreciate any help! I still a total newbie, so sorry if it seems like an easy question. but this is bending my mind!
Input(n): 4
Output: [[4],[3,1],[1,3],[2,2],[1,1,2],[1,2,1],[2,1,1],[1,1,1,1]]
import java.util.*;
import javax.naming.PartialResultException;
public class Perm {
private List<List<Integer>> computePerm(int n) {
// I totally lost it here
if (n == 0) {
return computePerm(0);
} else {
List<Integer> arr2 = new ArrayList<>();
for (List<Integer> entry : arr1) {
for (int i = 0; i < entry.size(); i++) {
arr2.add(entry.get(i)); // This obviously doesn't work
}
}
}
return computePerm(n);
}
public static void main(String[] args) {
Perm perm1 = new Perm();
System.out.println(computePerm(4));
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…