90. Subsets II
Description
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 .
Example 1:
1 2
| Input: nums = [1,2,2] Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
|
Example 2:
1 2
| Input: nums = [0] Output: [[],[0]]
|
Constraints:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
Hints/Notes
- 2024/12/25
- backtracking
- No solution from 0x3F
Solution
Language: C++
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
| class Solution { public: vector<vector<int>> res; vector<int> path;
vector<vector<int>> subsetsWithDup(vector<int>& nums) { ranges::sort(nums); dfs(0, nums); return res; }
void dfs(int index, vector<int>& nums) { if (index == nums.size()) { res.push_back(path); return; } path.push_back(nums[index]); dfs(index + 1, nums); path.pop_back(); int start = index + 1; while (start < nums.size() && nums[start] == nums[index]) { start++; } dfs(start, nums); } };
|