Skip to content

Commit 6e5de0b

Browse files
authored
Create 78. Subsets (#484)
2 parents a1a3762 + 9a5b905 commit 6e5de0b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

78. Subsets

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> subsets(vector<int>& nums) {
4+
vector<vector<int>> result;
5+
vector<int> curr;
6+
7+
function<void(int)> explore = [&](int index) {
8+
if (index == nums.size()) {
9+
result.push_back(curr);
10+
return;
11+
}
12+
13+
// Include the current element
14+
curr.push_back(nums[index]);
15+
explore(index + 1);
16+
curr.pop_back(); // Backtrack
17+
18+
// Exclude the current element
19+
explore(index + 1);
20+
};
21+
22+
explore(0);
23+
return result;
24+
}
25+
};

0 commit comments

Comments
 (0)