LeetCode39 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. (Medium)

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [2, 3, 6, 7] and target 7
A solution set is:

[
  [7],
  [2, 2, 3]
]

分析:

先排个序,用DFS搜索,每个数可选可不选,然后在start > end或者candidates[start] > target后就return。

恰好candidates[start] = target满足时添加到结果中。

代码:

 1 class Solution {
 2 private:
 3     vector<vector<int>>result;
 4     void dfs(int start, int end, const vector<int>& candidates, int target, vector<int>& internal) {
 5         if (start > end) {
 6             return;
 7         }
 8         if (candidates[start] == target) {
 9             internal.push_back(candidates[start]);
10             result.push_back(internal);
11             internal.pop_back();
12             return;
13         }
14         if (candidates[start] > target) {
15             return;
16         }
17         dfs(start + 1, end, candidates, target, internal);
18         internal.push_back(candidates[start]);
19         dfs(start, end, candidates, target - candidates[start], internal);
20         internal.pop_back();
21     }
22 public:
23     vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
24         sort(candidates.begin(), candidates.end());
25         int end = candidates.size() - 1;
26         vector<int> internal;
27         dfs(0, end, candidates, target, internal);
28         return result;
29     }
30 };
时间: 2024-10-19 16:28:19

LeetCode39 Combination Sum的相关文章

leetcode笔记:Combination Sum III

一. 题目描述 Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. E

leetcode笔记:Combination Sum II

一. 题目描述 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 once number of times. Note: ? All numbers (including target)

【转载】Combination Sum

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 (includi

【leetcode】Combination Sum

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 (includi

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/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

LeetCode40 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 target) will be

LeetCode Combination Sum III

原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 与Combination Sum II相似,不同的是中不是所有元素相加,只是k个元素相加. 所以在把item的copy 加到res前需要同时满足item.size() == k 和 target == 0两个条件. AC Java: 1 public class Solution { 2 public List<List<Integer>> combinationS

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

[LeetCode][JavaScript]Combination Sum III

Combination Sum III Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascend