forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubsetsII.java
27 lines (24 loc) · 856 Bytes
/
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
// https://leetcode.com/problems/subsets-ii
// T: O(2^n)
// S: O(n * 2^n)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class SubsetsII {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
final List<List<Integer>> result = new ArrayList<>();
helper(result, new LinkedList<>(), nums, 0);
return result;
}
public void helper(List<List<Integer>> result, LinkedList<Integer> current, int[] numbers, int index) {
result.add(new ArrayList<>(current));
for(int i = index ; i < numbers.length ; i++) {
if(i > index && numbers[i] == numbers[i-1]) continue;
current.add(numbers[i]);
helper(result, current, numbers, i + 1);
current.removeLast();
}
}
}