s0078
All checks were successful
ci/woodpecker/push/test Pipeline was successful

This commit is contained in:
Sainnhe Park 2023-02-02 18:53:00 +08:00
parent 06929474ee
commit ca4fe2293d
4 changed files with 41 additions and 0 deletions

13
include/s0078_subsets.hpp Normal file
View File

@ -0,0 +1,13 @@
#ifndef S0078_SUBSETS_HPP
#define S0078_SUBSETS_HPP
#include <vector>
using namespace std;
class S0078 {
public:
vector<vector<int>> subsets(vector<int>& nums);
};
#endif

View File

@ -43,6 +43,7 @@
- [总结](./backtrack.md)
- [组合问题](./combinations.md)
- [切割问题](./split.md)
- [子集问题](./subsets.md)
# STL

5
notes/src/subsets.md Normal file
View File

@ -0,0 +1,5 @@
# 子集问题
## [78. 子集](https://leetcode.cn/problems/subsets/)
其实和切割问题非常像。

22
src/s0078_subsets.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "s0078_subsets.hpp"
void subsetsDFS(vector<int> &subset, vector<vector<int>> &result, vector<int> &nums, int startIndex) {
int size = nums.size();
// 结束条件
if (startIndex == size) return;
for (int i = startIndex; i < size; ++i) {
subset.push_back(nums[i]);
result.push_back(subset);
subsetsDFS(subset, result, nums, i + 1);
subset.pop_back();
}
}
vector<vector<int>> S0078::subsets(vector<int>& nums) {
vector<int> subset{};
vector<vector<int>> result{};
subsetsDFS(subset, result, nums, 0);
result.push_back(vector<int>{});
return result;
}