Java:如何实现子集的子集?

标签 java algorithm set subset

以下实现找到集合的子集,但任何人都可以解释一下 if((i&(1<<j)) > 0)正在做,为什么?

评论似乎没有帮助并尝试了控制台日志记录,但仍然很难看出它到底在做什么。

//Print all subsets of given set[]
static void printSubsets(char set[]) {
    int n = set.length;

    //Run a loop for printing all 2^n subsets one by one
    for(int i=0; i<(1<<n); i++) {
        System.out.print("{ ");

        //Print current subset 
        for(int j=0; j<n; j++) {

            //(1<<j) is a number with jth bit 1
            //so when we 'and' them with the 
            //subset number we get which numbers
            //are present in the subset and which are not
            if((i&(1<<j)) > 0) {
                System.out.print(set[j] + " ");
            }
        }
            System.out.println("}");
    }
}

public static void main(String args[]) {
    char set[] = {'a', 'b', 'c'};
    printSubsets(set);
}

最佳答案

在一个子集中,每个元素可能存在,也可能不存在。所以每个元素只有 2 种可能的状态:in 或 out。 1 或 0。如果我们查看从 0 到 2^n -1 的数字的二进制表示, 其中n是元素的数量,例如 n=3 ,我们有:

    cba
0 = 000
1 = 001
2 = 010
3 = 011
4 = 100
5 = 101
6 = 110
7 = 111

有8个可能的子集,位代表一个元素是否在子集中。 这是程序使用的思路:

  • 外循环来自0直到 2^n-1 .
  • 内部循环来自0直到 n-1 .
  • 1<<j是 1 向左移动 j次。

例如,当i=3 ,对应于位 011 . 我们从 0 开始循环直到 2 , 比较 i反对001 , 010 , 和 100 . 对于这些值,表达式 i & (1 << j)将被评估为 011 & 001 = 001 , 011 & 010 = 010 , 和 011 & 100 = 000 , 分别。 前两个大于 0,最后一个不是。 所以System.out.print(set[j] + " ")将打印 ab .

关于Java:如何实现子集的子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40702926/

相关文章:

java - 如何使用 ExecutorService Java 减少到达 Runnable 类的运行方法的时间延迟

java - 这里正确的架构设计是什么?

algorithm - Quicksort 是 "adaptive"和 "online"吗?

java - 合并两个列表,不重复

c++ - 何时使用 std::unordered_set 而不是 std::set?

java - 运行时依赖项错误 "mvn compile"

mac终端下javac命令输出乱码

java - 添加到集合 O(n)?

java - Hibernate:更新不同 session 中的对象?

java - 已排序数组的快速排序堆栈溢出(适用于其他数据集)