LintCode: Combination Sum II

C++

DFS

 1 class Solution {
 2 public:
 3     void help(vector<int> &a, int now, int sum, int target, vector<int> &path, vector<vector<int> > &ans, bool last) {
 4         if (sum > target) {
 5             return ;
 6         }
 7         if (now >= a.size()) {
 8             if (sum == target) {
 9                 ans.push_back(path);
10             }
11             return ;
12         }
13         if ((now == 0) || (a[now - 1] != a[now]) || last) {
14             path.push_back(a[now]);
15             help(a, now + 1, sum + a[now], target, path, ans, true);
16             path.pop_back();
17         }
18         help(a, now + 1, sum, target, path, ans, false);
19     }
20     /**
21      * @param num: Given the candidate numbers
22      * @param target: Given the target number
23      * @return: All the combinations that sum to target
24      */
25     vector<vector<int> > combinationSum2(vector<int> &num, int target) {
26         // write your code here
27         sort(num.begin(), num.end());
28         vector<int> path;
29         vector<vector<int> > ans;
30         help(num, 0, 0, target, path, ans, true);
31         return ans;
32     }
33 };
时间: 2024-10-07 05:02:18

LintCode: Combination Sum II的相关文章

【leetcode】Combination Sum II

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t

LeetCode: Combination Sum II 解题报告

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note:All numbers (including ta

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

[LeetCode][JavaScript]Combination Sum II

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t

LeetCode 040 Combination Sum II

题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (includ

Combination Sum II leetcode java

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

040 Combination Sum II

040 Combination Sum II 这道题是 039 Combination Sum 的延伸, 稍作改动就好 class Solution: # @param {integer[]} candidates # @param {integer} target # @return {integer[][]} def combinationSum2(self, candidates, target): candidates.sort() ans = [] self.help(candidat

Combination Sum,Combination Sum II,Combination Sum III

39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (inc

Lintcode: k Sum II

Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target. Example Given [1,2,3,4], k=2, target=5, [1,4] and [2,3] are possible solutions. 这道题同Combination Sum II 1 public class Solution { 2 /** 3