-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00090-subsets_II.java
38 lines (30 loc) · 1.02 KB
/
00090-subsets_II.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
34
35
36
37
38
// 90: Subsets Ii
// https://leetcode.com/problems/subsets-ii
import java.util.List;
import java.util.ArrayList;
class Solution {
private void backtrack(int[] nums, int start, List<Integer> current, List<List<Integer>> result) {
result.add(new ArrayList<Integer>(current));
for (int i = start; i < nums.length; i++) {
if (i>start && nums[i] == nums[i-1]) continue;
current.add(nums[i]);
backtrack(nums, i + 1, current, result);
current.remove(current.size() - 1);
}
}
// SOLUTION
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> current = new ArrayList<>();
backtrack(nums, 0, current, result);
return result;
}
public static void main(String[] args) {
Solution o = new Solution();
// INPUT
int[] nums = {1,2,2};
// OUTPUT
var result = o.subsetsWithDup(nums);
System.out.println(result);
}
}