标签:return gets push space 使用 可重复 names art bsp
给定一个int数组A,数组中元素互不重复,给定一个数x,求所有求和能得到x的数字组合,组合中的元素来自A,可重复使用。
#include<iostream> #include<vector> #include<algorithm> using namespace std; void getSet(vector<int>& nums, vector<vector<int> >& res, vector<int> &tem, int target, int index); vector<vector<int> > getSetOfSum(vector<int>& nums, int target) { vector<vector<int> > res; if (nums.size() == 0) return {}; sort(nums.begin(), nums.end()); vector<int> tem; getSet(nums, res, tem, target, 0); return res; } void getSet(vector<int>& nums, vector<vector<int> >& res, vector<int> &tem, int target, int index) { if (target == 0) res.push_back(tem); for (int i = index; i < int(nums.size()); i++) { if (target - nums[i] < 0) return; tem.push_back(nums[i]); getSet(nums, res, tem, target - nums[i], i); tem.pop_back(); } } int main() { vector<vector<int> >ans; vector<int>nums = { 3,2,6,7 }; int target = 7; ans = getSetOfSum(nums, target); int i = ans.size(); for (int j = 0; j < i; j++) { for (auto v : ans[j]) { cout << v << " "; } cout << endl; } system("pause"); return 0; }
标签:return gets push space 使用 可重复 names art bsp
原文地址:https://www.cnblogs.com/r1-12king/p/13378466.html