码迷,mamicode.com
首页 > 其他好文 > 详细

Combination Sum | & ||

时间:2016-07-05 06:25:29      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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.

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

 Notice
  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.
Example

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

分析:递归

 1 public class Solution {
 2     /**
 3      * @param candidates: A list of integers
 4      * @param target:An integer
 5      * @return: A list of lists of integers
 6      */
 7     public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
 8         ArrayList<Integer> list = new ArrayList<Integer>();
 9         ArrayList<ArrayList<Integer>> listsAll = new ArrayList<ArrayList<Integer>>();
10         Arrays.sort(candidates);
11         helper(0, 0, candidates, target, list, listsAll);
12         return listsAll;
13     }
14     
15     public static void helper(int index, int total, int[] candidates, int target, ArrayList<Integer> list, ArrayList<ArrayList<Integer>> listsAll) {
16         if (index >= candidates.length || total >= target) return;
17         list.add(candidates[index]);
18         total += candidates[index];
19         if (total == target) {
20             listsAll.add(new ArrayList<Integer>(list));
21         }
22         helper(index, total, candidates, target, list, listsAll);
23         total = total - list.get(list.size() - 1);
24         list.remove(list.size() - 1);
25         helper(index + 1, total, candidates, target, list, listsAll);        
26     }
27 }

 

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.

 Notice
  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.
Example

Given candidate set [10,1,6,7,2,1,5] and target 8,

A solution set is:

[
  [1,7],
  [1,2,5],
  [2,6],
  [1,1,6]
]
 1 public class Solution {
 2     /**
 3      * @param num: Given the candidate numbers
 4      * @param target: Given the target number
 5      * @return: All the combinations that sum to target
 6      */
 7     
 8     public ArrayList<ArrayList<Integer>> combinationSum2(int[] candidates, int target) {
 9         ArrayList<Integer> list = new ArrayList<Integer>();
10         ArrayList<ArrayList<Integer>> listsAll = new ArrayList<ArrayList<Integer>>();
11         Arrays.sort(candidates);
12         helper(0, 0, candidates, target, list, listsAll);
13         return listsAll;
14     }
15     
16     public static void helper(int index, int total, int[] candidates, int target, ArrayList<Integer> list, ArrayList<ArrayList<Integer>> listsAll) {
17         if (index >= candidates.length || total >= target) return;
18         list.add(candidates[index]);
19         total += candidates[index];
20         if (total == target) {
21             listsAll.add(new ArrayList<Integer>(list));
22         }
23         helper(index + 1, total, candidates, target, list, listsAll);
24         total = total - list.get(list.size() - 1);
25         list.remove(list.size() - 1);
26         while (index + 1 < candidates.length && candidates[index] == candidates[index + 1]) {
27             index++;
28         }
29         helper(index + 1, total, candidates, target, list, listsAll);        
30     }
31 }

参考请注明出处:cnblogs.com/beiyeqingteng/ 

Combination Sum | & ||

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5642221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!