Skip to content

Commit 7373b88

Browse files
committed
day 3
1 parent 0b773e0 commit 7373b88

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Subsets II
3+
==========
4+
5+
Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
6+
7+
The solution set must not contain duplicate subsets. Return the solution in any order.
8+
9+
Example 1:
10+
Input: nums = [1,2,2]
11+
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
12+
13+
Example 2:
14+
Input: nums = [0]
15+
Output: [[],[0]]
16+
17+
Constraints:
18+
1 <= nums.length <= 10
19+
-10 <= nums[i] <= 10
20+
*/
21+
22+
class Solution
23+
{
24+
public:
25+
void dfs(vector<int> &nums, int i, vector<int> atn, set<vector<int>> &ans)
26+
{
27+
if (i == nums.size())
28+
{
29+
sort(atn.begin(), atn.end());
30+
ans.insert(atn);
31+
return;
32+
}
33+
34+
dfs(nums, i + 1, atn, ans);
35+
atn.push_back(nums[i]);
36+
dfs(nums, i + 1, atn, ans);
37+
}
38+
39+
vector<vector<int>> subsetsWithDup(vector<int> &nums)
40+
{
41+
set<vector<int>> sans;
42+
dfs(nums, 0, {}, sans);
43+
vector<vector<int>> ans;
44+
for (auto &i : sans)
45+
ans.push_back(i);
46+
return ans;
47+
}
48+
};

Leetcode Daily Challenge/August-2021/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
| :-: | :------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------: |
55
| 1. | [Making A Large Island](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3835/) | [cpp](./01.%20Making%20A%20Large%20Island.cpp) |
66
| 2. | [Two Sum](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3836/) | [cpp](./02.%20Two%20Sum.cpp) |
7+
| 3. | [Subsets I](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/613/week-1-august-1st-august-7th/3837/) | [cpp](./03.%20Subsets%20II.cpp) |

0 commit comments

Comments
 (0)