-
Notifications
You must be signed in to change notification settings - Fork 8
/
SubsetsII.java
43 lines (40 loc) · 1.21 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
34
35
36
37
38
39
40
41
42
43
package permutationcombination;
// Source : https://leetcode.com/problems/subsets-ii/
// Id : 90
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2021/2/20
// Topic : combination
// Level : Medium
// Other :
// Tips :
// Links : 39,78
// Result : 100.00% 24.48%
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SubsetsII {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
backtracking(0, nums, false, new ArrayList<>(), ans);
return ans;
}
private void backtracking(int start, int[] nums, boolean pre, List<Integer> path, List<List<Integer>> ans) {
if (start == nums.length) {
ans.add(new ArrayList<>(path));
return;
}
/**
* 11
* 10
* 01 排除此类重复情况
* 00
*/
if (pre || start == 0 || nums[start - 1] != nums[start]) {
path.add(nums[start]);
backtracking(start + 1, nums, true, path, ans);
path.remove(path.size() - 1);
}
backtracking(start + 1, nums, false, path, ans);
}
}