public HashSet<HashSet<Integer>> getAllSubset(HashSet<Integer> set) {
int[] superSet = getHashToIntArray(set);
double max = Math.pow(2, superSet.length);
HashSet<HashSet<Integer>> result = new HashSet<HashSet<Integer>>();
for(int i=1; i<=max; i++) {
int n=i;
HashSet<Integer> subset = new HashSet<Integer>();
for(int j=0; j<superSet.length; j++) {
if(n%2==1)
subset.add(superSet[j]);
n=n/2;
}
result.add(subset);
}
return result;
}
private int[] getHashToIntArray(HashSet<Integer> set) {
int[] result = new int[set.size()];
int i=0;
for(Integer n: set) {
result[i]=n;
i++;
}
return result;
}
No comments:
Post a Comment