Power set: Difference between revisions
Content deleted Content added
m →{{header|PARI/GP}}: lang tag |
|||
Line 889:
[[Category:Recursion]]
This implementation sorts each subset, but not the whole list of subsets (which would require a custom comparator). It also destroys the original set.
<lang java5>public static ArrayList<
{
if(n<0)
{
return null;
}
if(n==0)
{
if(ps==null)
ps=new ArrayList();
ps.add(" ");
return ps;
}
ps=getpowerset(a, n-1, ps);
ArrayList<String> tmp=new ArrayList<String>();
for(String s:ps)
{
if(s.equals(" "))
tmp.add(""+a[n-1]);
else
return ans;▼
tmp.add(s+a[n-1]);
}</lang>▼
}
ps.addAll(tmp);
▲ }</lang>
===Iterative===
The iterative implementation of the above idea. Each subset is in the order that the element appears in the input list. This implementation preserves the input.
|