forked from geemaple/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path90.subsets-ii.cpp
38 lines (29 loc) · 886 Bytes
/
90.subsets-ii.cpp
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
class Solution {
private:
void dfsTraversal(vector<int>& nums, vector<int> &tmp, int start, vector<vector<int>> &result){
result.push_back(tmp);
for (auto i = start; i < nums.size(); ++i)
{
if (i > start && nums[i] == nums[i - 1])
{
continue;
}
tmp.push_back(nums[i]);
dfsTraversal(nums, tmp, i + 1, result);
tmp.pop_back();
}
}
public:
// DFS
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
if (nums.size() == 0)
{
return vector<vector<int>>();
}
sort(nums.begin(), nums.end());
vector<int> tmp;
vector<vector<int>> result;
dfsTraversal(nums, tmp, 0, result);
return result;
}
};