Obtaining a powerset of a set in Java

后端 未结 26 1666
青春惊慌失措
青春惊慌失措 2020-11-22 11:33

The powerset of {1, 2, 3} is:

{{}, {2}, {3}, {2, 3}, {1, 2}, {1, 3}, {1, 2, 3}, {1}}

Let\'s say I have a Set in Java:<

26条回答
  •  囚心锁ツ
    2020-11-22 11:43

    One way without recursion is the following: Use a binary mask and make all the possible combinations.

    public HashSet createPowerSet(Object[] array)
    {
        HashSet powerSet=new HashSet();
        boolean[] mask= new boolean[array.length];
    
        for(int i=0;i

提交回复
热议问题