我想要做的如下:
输入:n,例如 n = 3
输出:{000, 001, 010, 011, 100, 101, 110, 111},生成所有子集,我不在乎子集的顺序
我已经实现了一个算法:
for (long i = 0, max = 1 << n; i < max; i++) {
for (int j = 0; j < n; j++) {
// check if the j bit is set to 1
int isSet = (int) i & 1 << j;
if (isSet > 0) {
// if yes, set the corresponding jth bit of x as 1.
}
}
// add x to results;
}
我知道格雷码可以做这样的事情。但我想知道哪一种是生成子集的最有效方法?