-
Notifications
You must be signed in to change notification settings - Fork 521
/
Copy pathSubsets.cpp
45 lines (37 loc) · 796 Bytes
/
Subsets.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
39
40
41
42
43
44
45
/*
Subsets
=======
Given an integer array nums, return all possible subsets (the power set).
The solution set must not contain duplicate subsets.
Example 1:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [0]
Output: [[],[0]]
Constraints:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
*/
class Solution
{
void subsets(vector<vector<int>> &ans, vector<int> nums, vector<int> out = {}, int i = 0)
{
if (i >= nums.size())
{
ans.push_back(out);
return;
}
subsets(ans, nums, out, i + 1);
out.push_back(nums[i]);
subsets(ans, nums, out, i + 1);
out.pop_back();
}
public:
vector<vector<int>> subsets(vector<int> &nums)
{
vector<vector<int>> ans;
subsets(ans, nums);
return ans;
}
};