【发布时间】:2017-01-25 02:02:16
【问题描述】:
我刚刚发现了一种寻找幂集的算法。我在谷歌上搜索了解决方案,但没有找到任何好的解决方案,所以我自己想出了一个。但我想知道它是什么算法,因为我在网上或任何书中都找不到它。我的意思是,它有名字吗?与我在一些网站上找到的计算幂集的算法相比,我认为我的要好得多,不知道为什么没有人使用它?
这是算法:
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);
}
}
【问题讨论】: