This commit is contained in:
parent
06929474ee
commit
ca4fe2293d
13
include/s0078_subsets.hpp
Normal file
13
include/s0078_subsets.hpp
Normal 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
|
@ -43,6 +43,7 @@
|
||||
- [总结](./backtrack.md)
|
||||
- [组合问题](./combinations.md)
|
||||
- [切割问题](./split.md)
|
||||
- [子集问题](./subsets.md)
|
||||
|
||||
# STL
|
||||
|
||||
|
5
notes/src/subsets.md
Normal file
5
notes/src/subsets.md
Normal file
@ -0,0 +1,5 @@
|
||||
# 子集问题
|
||||
|
||||
## [78. 子集](https://leetcode.cn/problems/subsets/)
|
||||
|
||||
其实和切割问题非常像。
|
22
src/s0078_subsets.cpp
Normal file
22
src/s0078_subsets.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user