# [LeetCode] 39. Combination Sum 组合总和

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Example 1:

Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.


Example 2:

Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]


Example 3:

Input: candidates = [2], target = 1
Output: []

Constraints:

• 1 <= candidates.length <= 30
• 2 <= candidates[i] <= 40
• All elements of candidates are distinct.
• 1 <= target <= 40

class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> cur;
dfs(candidates, target, 0, cur, res);
return res;
}
void dfs(vector<int>& candidates, int target, int start, vector<int>& cur, vector<vector<int>>& res) {
if (target < 0) return;
if (target == 0) { res.push_back(cur); return; }
for (int i = start; i < candidates.size(); ++i) {
cur.push_back(candidates[i]);
dfs(candidates, target - candidates[i], i, cur, res);
cur.pop_back();
}
}
};

class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> res;
sort(candidates.begin(), candidates.end());
for (int i = 0; i < candidates.size(); ++i) {
if (candidates[i] > target) break;
if (candidates[i] == target) {res.push_back({candidates[i]}); break;}
vector<int> vec = vector<int>(candidates.begin() + i, candidates.end());
vector<vector<int>> tmp = combinationSum(vec, target - candidates[i]);
for (auto a : tmp) {
a.insert(a.begin(), candidates[i]);
res.push_back(a);
}
}
return res;
}
};

class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<vector<int>>> dp(target + 1);
sort(candidates.begin(), candidates.end());
for (int i = 1; i <= target; ++i) {
vector<vector<int>> cur;
for (int j = 0; j < candidates.size(); ++j) {
if (candidates[j] > i) break;
if (candidates[j] == i) {
cur.push_back({candidates[j]});
break;
}
for (auto a : dp[i - candidates[j]]) {
if (candidates[j] > a[0]) continue;
a.insert(a.begin(), candidates[j]);
cur.push_back(a);
}
}
dp[i] = cur;
}
return dp[target];
}
};

Github 同步地址：

https://github.com/grandyang/leetcode/issues/39

Combination Sum III

Combination Sum II

Combinations

Letter Combinations of a Phone Number

https://leetcode.com/problems/combination-sum/

https://leetcode.com/problems/combination-sum/discuss/16825/Recursive-java-solution

https://leetcode.com/problems/combination-sum/discuss/16509/Iterative-Java-DP-solution

https://leetcode.com/problems/combination-sum/discuss/16502/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)

LeetCode All in One 题目讲解汇总(持续更新中...)

posted @ 2015-04-12 12:03  Grandyang  阅读(37708)  评论(18编辑  收藏  举报