Obtaining a powerset of a set in Java

后端 未结 26 2105
青春惊慌失措
青春惊慌失措 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:44

    Actually, I've written code that does what you're asking for in O(1). The question is what you plan to do with the Set next. If you're just going to call size() on it, that's O(1), but if you're going to iterate it that's obviously O(2^n).

    contains() would be O(n), etc.

    Do you really need this?

    EDIT:

    This code is now available in Guava, exposed through the method Sets.powerSet(set).

提交回复
热议问题