题解 | #加起来和为目标值的组合(二)#
加起来和为目标值的组合(二)
https://www.nowcoder.com/practice/75e6cd5b85ab41c6a7c43359a74e869a
#include <vector> class Solution { public: vector<vector<int> > result; vector<vector<int> > combinationSum2(vector<int> &num, int target) { if (num.empty()) return result; sort(num.begin(), num.end()); vector<int> tmp; back_tracking(num, tmp, 0, 0, target); return result; } void back_tracking(vector<int>& num, vector<int>& tmp, int cur_sum, int begin, int target) { if (cur_sum >= target) { if (cur_sum == target) { result.push_back(tmp); } return; } for (int i = begin; i < num.size(); ++i) { if (i > begin && num[i] == num[i-1]) continue; tmp.push_back(num[i]); back_tracking(num, tmp, cur_sum + num[i], i + 1, target); tmp.pop_back(); } return; } };
2023-剑指-回溯 文章被收录于专栏
2023-剑指-回溯