39、
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:
For example, given candidate set 2,3,6,7
and target 7
,
A solution set is:
[7]
[2, 2, 3]
解题思路:
先排好序,然后每次递归中把剩下的元素一一加到结果集合中,并且把目标减去加入的元素,然后把剩下元素(包括当前加入的元素)放到下一层递归中解决子问题。算法复杂度因为是NP问题,所以是指数量级的。
代码中for循环中有一个判断,是为了去除重复元素产生重复结果的影响,因为数可以重复使用,所以重复的元素也就没有作用了
public class Solution { public IList<IList<int>> CombinationSum(int[] candidates, int target) { Array.Sort(candidates); IList<IList<int>> res =new List<IList<int>>(); IList<int> combination=new List<int>(); Fun(candidates, target, res, combination, 0); return res; } private void Fun(int[] candidates, int target, IList<IList<int>> res, IList<int> combination, int begin) { if (target==0) { res.Add(new List<int>(combination)); return; } for (int i = begin; i != candidates.Length && target >= candidates[i]; i++) { if (i > 0 && candidates[i] == candidates[i - 1]) continue; combination.Add(candidates[i]); Fun(candidates, target - candidates[i], res, combination, i); combination.RemoveAt(combination.Count-1); } } }
public class Solution { public IList<IList<int>> CombinationSum2(int[] candidates, int target) { Array.Sort(candidates); IList<IList<int>> res =new List<IList<int>>(); IList<int> combination=new List<int>(); Fun(candidates, target, res, combination, 0); return res; } private void Fun(int[] candidates, int target, IList<IList<int>> res, IList<int> combination, int begin) { if (target==0) { res.Add(new List<int>(combination)); return; } if (begin >= candidates.Length) return; for (int i = begin; i < candidates.Length && target >= candidates[i]; i++) { if (i > begin && candidates[i] == candidates[i - 1]) continue; combination.Add(candidates[i]); Fun(candidates, target - candidates[i], res, combination, i+1); combination.RemoveAt(combination.Count-1); } } }
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:
Input: k = 3, n = 7
Output:[[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output:[[1,2,6], [1,3,5], [2,3,4]]
继续使用上面的思路很容易得到解答:
public class Solution { public IList<IList<int>> CombinationSum3(int k, int n) { int[] nums = new int[] {1,2,3,4,5,6,7,8,9}; IList<IList<int>> res =new List<IList<int>>(); IList<int> list=new List<int>(); Fun(nums, n, res, list, 0,k); return res; } private void Fun(int[] nums, int n, IList<IList<int>> res, IList<int> list, int begin,int k) { if (n == 0 && k==0) { res.Add(new List<int>(list)); return; } if (begin >= 9 || k<0) return; for (int i = begin; i < 9 && n >= nums[i]; i++) { list.Add(nums[i]); Fun(nums, n - nums[i], res, list, i + 1,k-1); list.RemoveAt(list.Count - 1); } } }
public class Solution { public IList<IList<int>> CombinationSum3(int k, int n) { IList<IList<int>> res =new List<IList<int>>(); IList<int> list=new List<int>(); Fun(n, res, list,1,k); return res; } private void Fun(int n, IList<IList<int>> res, IList<int> list, int begin,int k) { if (n == 0 && k==0) { res.Add(new List<int>(list)); return; } if (begin >= 10|| k<0) return; for (int i = begin; i < 10 ; i++) { list.Add(i); Fun(n - i, res, list, i + 1,k-1); list.RemoveAt(list.Count - 1); } } }