diff --git a/include/s0078_subsets.hpp b/include/s0078_subsets.hpp new file mode 100644 index 0000000..a1cfed2 --- /dev/null +++ b/include/s0078_subsets.hpp @@ -0,0 +1,13 @@ +#ifndef S0078_SUBSETS_HPP +#define S0078_SUBSETS_HPP + +#include + +using namespace std; + +class S0078 { + public: + vector> subsets(vector& nums); +}; + +#endif diff --git a/notes/src/SUMMARY.md b/notes/src/SUMMARY.md index 9c602c4..600c238 100644 --- a/notes/src/SUMMARY.md +++ b/notes/src/SUMMARY.md @@ -43,6 +43,7 @@ - [总结](./backtrack.md) - [组合问题](./combinations.md) - [切割问题](./split.md) +- [子集问题](./subsets.md) # STL diff --git a/notes/src/subsets.md b/notes/src/subsets.md new file mode 100644 index 0000000..4eaa8ac --- /dev/null +++ b/notes/src/subsets.md @@ -0,0 +1,5 @@ +# 子集问题 + +## [78. 子集](https://leetcode.cn/problems/subsets/) + +其实和切割问题非常像。 diff --git a/src/s0078_subsets.cpp b/src/s0078_subsets.cpp new file mode 100644 index 0000000..ad2cf92 --- /dev/null +++ b/src/s0078_subsets.cpp @@ -0,0 +1,22 @@ +#include "s0078_subsets.hpp" + +void subsetsDFS(vector &subset, vector> &result, vector &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> S0078::subsets(vector& nums) { + vector subset{}; + vector> result{}; + subsetsDFS(subset, result, nums, 0); + result.push_back(vector{}); + return result; +}