Skip to content

Commit 12f50ce

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 16.30 MB (Top 20.62%)
1 parent 7e19890 commit 12f50ce

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed
+20-24
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 16.30 MB (Top 20.62%)
2+
3+
// Recursive Solution
4+
// Time complexity : O(N*(2^N))
5+
// Space complexity : O(N*(2^N))
16
class Solution {
2-
void subsetGenerator (vector<int> nums, int n, vector<vector<int>> &ans, int i, vector<int> subset)
3-
{
4-
5-
if(i>=n) //Base Case
6-
{
7-
ans.push_back(subset); //the subset obatined is pushed into ans
8-
return ;
7+
public:
8+
vector<vector<int>> ans;
9+
10+
void sub(vector<int> &nums, int i, vector<int> temp)
11+
{
12+
if(i==nums.size())
13+
{
14+
ans.push_back(temp);
15+
return;
916
}
10-
//including the element at index i and then calling the recursive function
11-
subset.push_back(nums[i]);
12-
solve(nums,n,ans,i+1,subset);
1317

14-
15-
//excluding the element at index i and then calling the recursive function
16-
subset.pop_back();
17-
solve(nums,n,ans,i+1,subset);
18-
18+
sub(nums, i+1, temp);
19+
temp.push_back(nums[i]);
20+
sub(nums, i+1, temp);
1921
}
20-
public:
21-
22+
2223
vector<vector<int>> subsets(vector<int>& nums) {
23-
int i=0; // index is initialized to 0 as we start from the first element
24-
int n=nums.size(); // size of the vector nums
25-
vector<int> subset; // this vector will store each subset which is generated
26-
vector<vector<int>> ans; // will store all the subsets generated
27-
28-
subsetGenerator(nums, n, ans, i, subset);
29-
24+
vector<int> temp;
25+
sub(nums, 0, temp); // or sub(nums, 0, vector<int> {});
3026
return ans;
3127
}
3228
};

0 commit comments

Comments
 (0)