forked from jnozsc/lintcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets_II.cpp
More file actions
39 lines (35 loc) · 1.14 KB
/
Copy pathSubsets_II.cpp
File metadata and controls
39 lines (35 loc) · 1.14 KB
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
/*
Given a list of numbers that may has duplicate numbers, return all possible subsets
Note
Each element in a subset must be in non-descending order.
The ordering between two subsets is free.
The solution set must not contain duplicate subsets.
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > subsetsWithDup(const vector<int>& nums) {
vector<vector<int> > result;
// write your code here
vector<int> emptySubset;
result.push_back(emptySubset);
if (nums.size() == 0) {
return result;
}
vector<int> numsCopy(nums.begin(), nums.end());
sort(numsCopy.begin(), numsCopy.end());
for (int i = 0; i < numsCopy.size(); i++) {
int size = result.size();
for (int j = 0; j < size; j++) {
vector<int> newSubset = result[j];
newSubset.push_back(numsCopy[i]);
if (find(result.begin(), result.end(), newSubset) == result.end()) {
result.push_back(newSubset);
}
}
}
return result;
}
};