From 2080c40b204d8adcb70dd1c25a7b2bbba63c8b9c Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sun, 13 Feb 2022 15:47:25 +0800 Subject: [PATCH] add: 220213-CN --- 2202/220213.cpp | 35 +++++++++++++++++++++++++++++++++++ 2202/CMakeLists.txt | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 2202/220213.cpp diff --git a/2202/220213.cpp b/2202/220213.cpp new file mode 100644 index 0000000..02339f8 --- /dev/null +++ b/2202/220213.cpp @@ -0,0 +1,35 @@ +#include +#include + +/** + * 78. Subsets + * Given an integer array nums of unique elements, return all possible subsets (the power set). + * The solution set must not contain duplicate subsets. Return the solution in any order. + */ + +class Solution { +public: + static std::vector> subsets(const std::vector& nums) { + int s = (1 << nums.size()), n = nums.size(); + std::vector> ret(s); + for (int i = 0; i < s; ++i) { + std::vector& t = ret[i]; + t.reserve(n); + for (int j = 0; j < n; ++j) + if (i & (1 << j)) + t.push_back(nums[j]); + } + return ret; + } +}; + +int main() { + const auto ret = Solution::subsets({1,2,3}); + for (const auto& i : ret) { + for (const auto& j : i) { + std::printf("%d ", j); + } + std::printf("\n"); + } + return 0; +} diff --git a/2202/CMakeLists.txt b/2202/CMakeLists.txt index c9cf3eb..beda610 100644 --- a/2202/CMakeLists.txt +++ b/2202/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2202) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2202 220213-CN.cpp) +ADD_EXECUTABLE(2202 220213.cpp)