我刚刚发现了一种找到幂集的算法。我在网上搜索了解决方案,但没有找到好的方法,所以我自己想出了一个。但我想知道它是什么算法,因为我在网上或书中找不到它。我的意思是,它有名字吗?与我在某些网站上找到的计算幂集的算法相比,我认为我的算法更好,想知道为什么没有人使用它?
以下是该算法:
以下是该算法:
R <- []
L <- [ e1, e2 ... en ]
c <- 0
function: powerSet(L, c)
R <- R union L
for e in L starting at c
powerSet(L\{e}, c)
end
return R
end
以下是Java实现:
public static void powerSet(List<String> list, int count)
{
result.add(list);
for(int i = count; i < list.size(); i++)
{
List<String> temp = new ArrayList<String>(list);
temp.remove(i);
powerSet(temp, i);
}
}
O(2^n)
。@rejeep - phant0m