问题
Trying to figure out a way to generate a superset using java need simple answer to
what am looking for is
Input : ab
Output : {} {a} {b} {ab}
how do i go about this any clue ?
Thanks in advance
回答1:
int allMasks = (1 << N);
for (int i = 1; i < allMasks; i++)
{
for (int j = 0; j < N; j++)
if ((i & (1 << j)) > 0) //The j-th element is used
System.out.print((j + 1) + " ");
System.out.println();
}
回答2:
Since this is currently the second result in google for java superset I'm going to answer that query.
Set.addAll will do this.
example:
// A couple of sets
Set<Integer> set1 = new TreeSet<>(Arrays.asList(1, 2));
Set<Integer> set2 = new TreeSet<>(Arrays.asList(2, 3));
// Generate the superset
Set<Integer> superset = new TreeSet<>();
superset.addAll(set1); // {1, 2}
superset.addAll(set2); // {1, 2, 3}
来源:https://stackoverflow.com/questions/21620006/need-to-generate-superset-in-java