-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78.cpp
More file actions
29 lines (29 loc) · 937 Bytes
/
Copy path78.cpp
File metadata and controls
29 lines (29 loc) · 937 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
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
//List to store the final result
vector<vector<int>> list;
//Calculate the size of the power set
unsigned int pow_size = pow(2, nums.size());
//Counter is for looping through every possible combination
//j is a binary magic idenfier
//For example, 110 means that take the first element in nums
//and second element in nums without taking the third
int counter, j;
//From all possible solution
for(counter = 0; counter < pow_set_size; counter++)
{
vector<int> temp_list;
//Check the idenfier
for(j = 0; j < nums.size(); j++)
{
//If the digit is 1, then take it
if(counter & (1<<j))
temp_list.push_back(nums[j]);
}
//Push the whole set
list.push_back(temp_list);
}
return list;
}
};