Skip to content

Commit 8cacd61

Browse files
author
ishan.gupta
committed
all possible subset
1 parent 9d1626b commit 8cacd61

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

all-possible-subset.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using namespace std;
2+
#include <iostream>
3+
#include <bits/stdc++.h>
4+
5+
// https://leetcode.com/problems/subsets/
6+
// Can be done using bit manipulation also
7+
// 000
8+
// 001
9+
// 010
10+
// 011
11+
// 100 ....and so on
12+
13+
class Solution {
14+
public:
15+
void getAllSubsets(vector<vector<int>> &ans, vector<int> nums, vector<int> possibleSet, int indx){
16+
if(indx>=nums.size()){
17+
ans.push_back(possibleSet);
18+
return;
19+
}
20+
possibleSet.push_back(nums[indx]);
21+
getAllSubsets(ans,nums,possibleSet,indx+1);
22+
possibleSet.pop_back();
23+
getAllSubsets(ans,nums,possibleSet,indx+1);
24+
}
25+
26+
vector<vector<int>> subsets(vector<int>& nums) {
27+
vector<vector<int>> ans;
28+
vector<int> possibleSet = {};
29+
getAllSubsets(ans,nums,possibleSet,0);
30+
return ans;
31+
}
32+
};

0 commit comments

Comments
 (0)