/** * File: subset_sum_i_naive.cpp * Created Time: 2023-06-21 * Author: krahets (krahets@163.com) */ #include "../utils/common.hpp" /* 回溯算法:子集和 I */ void backtrack(vector &state, int target, int total, vector &choices, vector> &res) { // 子集和等于 target 时,记录解 if (total == target) { res.push_back(state); return; } // 遍历所有选择 for (size_t i = 0; i < choices.size(); i++) { // 剪枝:若子集和超过 target ,则跳过该选择 if (total + choices[i] > target) { continue; } // 尝试:做出选择,更新元素和 total state.push_back(choices[i]); // 进行下一轮选择 backtrack(state, target, total + choices[i], choices, res); // 回退:撤销选择,恢复到之前的状态 state.pop_back(); } } /* 求解子集和 I(包含重复子集) */ vector> subsetSumINaive(vector &nums, int target) { vector state; // 状态(子集) int total = 0; // 子集和 vector> res; // 结果列表(子集列表) backtrack(state, target, total, nums, res); return res; } /* Driver Code */ int main() { vector nums = {3, 4, 5}; int target = 9; vector> res = subsetSumINaive(nums, target); cout << "输入数组 nums = "; printVector(nums); cout << "target = " << target << endl; cout << "所有和等于 " << target << " 的子集 res = " << endl; printVectorMatrix(res); return 0; }