-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0090.子集-ii.java
75 lines (72 loc) · 1.52 KB
/
0090.子集-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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
* @lc app=leetcode.cn id=90 lang=java
*
* [90] 子集 II
*
* https://leetcode.cn/problems/subsets-ii/description/
*
* algorithms
* Medium (63.66%)
* Likes: 937
* Dislikes: 0
* Total Accepted: 245.4K
* Total Submissions: 385.4K
* Testcase Example: '[1,2,2]'
*
* 给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
*
* 解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
*
*
*
*
*
* 示例 1:
*
*
* 输入:nums = [1,2,2]
* 输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
*
*
* 示例 2:
*
*
* 输入:nums = [0]
* 输出:[[],[0]]
*
*
*
*
* 提示:
*
*
* 1
* -10
*
*
*
*
*/
// @lc code=start
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> results = new ArrayList<>();
backtrace(nums, results, new ArrayList<>(), 0);
return results;
}
private void backtrace(int[] nums, List<List<Integer>> results, List<Integer> trace, int index) {
results.add(new ArrayList<>(trace));
for (int i = index; i < nums.length; i++) {
if (i > index && nums[i] == nums[i - 1])
continue;
trace.add(nums[i]);
backtrace(nums, results, trace, i + 1);
trace.remove(trace.size() - 1);
}
}
}
// @lc code=end