-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubSets.cpp
More file actions
37 lines (35 loc) · 821 Bytes
/
subSets.cpp
File metadata and controls
37 lines (35 loc) · 821 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
35
36
37
/**
*Given a set of distinct integers, nums, return all possible subsets.
*Note:Elements in a subset must be in non-descending order.
*The solution set must not contain duplicate subsets.
*For example,
*If nums = [1,2,3], a solution is:
*[
* [3],
* [1],
* [2],
* [1,2,3],
* [1,3],
* [2,3],
* [1,2],
* []
*]
*/
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
int nums_len = nums.size();
int subsets_len = pow(2,nums_len);
sort(nums.begin(),nums.end());
vector<vector<int> > res(subsets_len,vector<int>());
for(int i = 0; i < subsets_len; i++)
{
for(int j = 0; j < nums_len; j++)
{
if(i >> j &1)
res[i].push_back(nums[j]);
}
}
return res;
}
};