-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSubsets.cpp
More file actions
34 lines (31 loc) · 896 Bytes
/
Copy pathSubsets.cpp
File metadata and controls
34 lines (31 loc) · 896 Bytes
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
/*
Given a set of distinct integers, return all possible subsets.
Note
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
*/
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > subsets(vector<int>& nums) {
vector<vector<int> > result;
// write your code here
vector<int> emptySet;
result.push_back(emptySet);
if (nums.size() == 0) {
return result;
}
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++) {
int size = result.size();
for (int j = 0; j < size; j++) {
vector<int> newSubset = result[j];
newSubset.push_back(nums[i]);
result.push_back(newSubset);
}
}
return result;
}
};