0
Find All Subsets from Given Set For Target Value.
I’m having trouble implementing the getSubset function to find the target value from the user input. Can anyone can help?
2 Antworten
+ 2
Can you share your code here?
0
static ArrayList<ArrayList<Integer>> getSubset(Integer[] set, int index){
ArrayList<ArrayList<Integer>>
allSubsets;
if( index < 0){
allSubsets = new ArrayList<>();
allSubsets.add(new ArrayList<>(); }
else {
all Subsets = getSubset(set, index -1);
Integer item = set[index];
ArrayList<ArrayList<Integer>> moreSubsets = new ArrayList<>();
allSubsets.stream().map((ArrayList<Integer> subset) -> {
ArrayList<Integer> newSubset = new ArrayList<>();
newSubset.addAll(subset);
return newSubset;
}) .map((newSubset) ->{ newSubset.add(item);
return newSubset;
}) .forEachOrdered((newSubset) -> { moreSubsets.add(newSubsets);
}); allSubsets.addAll(moreSubsets); }
return allSubsets; }
...
...
And once I ask the user for the target input im trying to call that function to retrieve all possible subset for target value.