From 3de8ed3745952f197775e8f533032b0300192fab Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Sat, 14 Oct 2023 10:18:35 -0700 Subject: [PATCH] day 15: 78-subsets --- 78-subsets/driver.cpp | 15 +++++++++++++++ 78-subsets/soln.cpp | 19 +++++++++++++++++++ 78-subsets/soln.hpp | 7 +++++++ 3 files changed, 41 insertions(+) create mode 100644 78-subsets/driver.cpp create mode 100644 78-subsets/soln.cpp create mode 100644 78-subsets/soln.hpp diff --git a/78-subsets/driver.cpp b/78-subsets/driver.cpp new file mode 100644 index 0000000..db7f5d7 --- /dev/null +++ b/78-subsets/driver.cpp @@ -0,0 +1,15 @@ +#include "soln.hpp" + +int main() { + vector nums{1,2,3}; + Solution soln; + vector> answer = soln.subsets(nums); + for(int i = 0; i < answer.size(); i++) { + cout << "Subset " << (i+1) << ": "; + for(int j = 0; j < answer[i].size(); j++) { + cout << answer[i][j] << " "; + } + cout << endl; + } + return 0; +} \ No newline at end of file diff --git a/78-subsets/soln.cpp b/78-subsets/soln.cpp new file mode 100644 index 0000000..c053333 --- /dev/null +++ b/78-subsets/soln.cpp @@ -0,0 +1,19 @@ +#include "soln.hpp" + +vector> Solution::subsets(vector &nums) +{ + int num_combos = 1 << nums.size(); + vector> subs(num_combos); + for (int i = 0; i < num_combos; i++) + { + for (int j = 0; j < nums.size(); j++) + { + // check if this index exists in this combo + if ((i >> j) & 1) + { + subs[i].push_back(nums[j]); + } + } + } + return subs; +} diff --git a/78-subsets/soln.hpp b/78-subsets/soln.hpp new file mode 100644 index 0000000..4e46f9c --- /dev/null +++ b/78-subsets/soln.hpp @@ -0,0 +1,7 @@ +#include +using namespace std; +class Solution +{ +public: + vector> subsets(vector &nums); +}; \ No newline at end of file