-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubsetsII.java
33 lines (31 loc) · 1.03 KB
/
SubsetsII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.namanh.bit_manipulation;
import java.util.*;
/**
* https://leetcode.com/problems/subsets-ii
* Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
* The solution set must not contain duplicate subsets. Return the solution in any order.
*
* S1: Sort array
* S2: Use set to store result
* S3: If array has n elements, we will have 2^n subsets
* S4: Each subset, index is bitmask of appearance (ex: 100, 010, 001...)
*
* Time: O(n * 2^n)
* Space: O(n * 2^n)
*/
public class SubsetsII {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
Set<List<Integer>> set = new HashSet<>();
int n = nums.length;
int numSubset = 1 << n;
for (int i = 0; i < numSubset; i++) {
List<Integer> list = new ArrayList<>();
for (int k = 0; k < n; k++) {
if (((i >> k) & 1) == 1) list.add(nums[k]);
}
set.add(list);
}
return new ArrayList<>(set);
}
}