LeetCode 39. Combination Sum

原题链接在这里:https://leetcode.com/problems/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.

It is guaranteed 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: []

Example 4:

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

Example 5:

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

题解:

dfs时, target减掉candidates里一个数,然后递归调用helper, target设成target - candidates[i]. recursion终止条件是target == 0, 加入item的copy, target < 0说明已经减多了,直接return.

backtracking时恢复原貌.

candidates里可能有duplicate, 所以需要去重.

去重可以在循环中加入if(i>0 && candidates[i] == candidates[i-1]) continue; 是因为本题允许使用重复元素,若后面元素相同则不能再进行此验算。并且起到了去重的作用,就不用眼看res中是否已有item了.

e.g. 若是candidates = [1,1,2], target 是 3, 则会返回多组 [1,1,1].

Time Complexity: exponential.

Space: O(size). res中最长item的size是recursion的stack space.

AC Java:

 1 public class Solution {
 2     public List<List<Integer>> combinationSum(int[] candidates, int target) {
 3         List<List<Integer>> res = new ArrayList<List<Integer>>();
 4         if(candidates == null || candidates.length == 0){
 5             return res;
 6         }
 7         Arrays.sort(candidates);
 8         helper(candidates,target,0,new ArrayList<Integer>(),res);
 9         return res;
10     }
11     private void helper(int[] candidates, int target, int start, List<Integer> item, List<List<Integer>> res){
12         if(target == 0){
13             res.add(new ArrayList<Integer>(item));
14             return;
15         }
16         if(target < 0){
17             return;
18         }
19         for(int i = start; i<candidates.length; i++){
20             //这里可以使用重复元素,所以若是下一个元素与之前元素相同则没有必要对下一个元素进项判断,浪费时间
21             if(i>start && candidates[i] == candidates[i-1]){
22                 continue;
23             }
24             item.add(candidates[i]);
25             helper(candidates,target-candidates[i],i,item,res);
26             item.remove(item.size()-1);
27         }
28     }
29 }

AC Python:

 1 class Solution:
 2     def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
 3         res = []
 4         self.dfs(candidates, target, 0, [], res)
 5         return res
 6     
 7     def dfs(self, candidates: List[int], target: int, start: int, item: List[int], res: List[List[int]]):
 8         if target == 0:
 9             res.append(item[:])
10             return
11         
12         for i in range(start, len(candidates)):
13             if candidates[i] <= target:
14                 item.append(candidates[i])
15                 self.dfs(candidates, target - candidates[i], i, item, res)
16                 item.pop()

类似CombinationsPermutationsN-Queens.

跟上Combination Sum IICombination Sum IIICombination Sum IV.

posted @ 2015-09-28 23:12  Dylan_Java_NYC  阅读(467)  评论(0编辑  收藏  举报