Combination Sum I and II

比较典型的helper的题目,现在我还搞不太清楚dfs之类的,只能用helper来统称了,你明白那个调调就行

public class Solution {
    public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
        ArrayList<ArrayList<Integer>> res =new ArrayList<ArrayList<Integer>>();
        Arrays.sort(candidates);
        if(candidates == null || candidates.length<1) return res;
        helper( candidates, target, res, new ArrayList<Integer>(), 0);
        return res;
    }
    public void helper(int[] c, int t,ArrayList<ArrayList<Integer>> res, ArrayList<Integer> item, int start ){
        if(0==t){
             res.add(new ArrayList<Integer> (item));//res.add(item);
            return; // 提速
        }
        if(0>t) return;
        for(int i=start; i< c.length; i++){
            if(i>start && c[i-1]==c[i]) continue;
            item.add(c[i]);
            helper(c, t-c[i], res, item, i); // 如果i变成i+1,则是不允许重复元素出现,而i,并且循环从i=start开始,则允许了重复元素构建target的可能性,一并解决了II
            item.remove(item.size()-1);
        }
    }
}
时间: 2024-12-16 09:14:05

Combination Sum I and II的相关文章

【leetcode】Combination Sum II

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 t

LeetCode: Combination Sum II 解题报告

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 ta

LeetCode: Combination Sum II [039]

[题目] 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 39 40 216 Combination Sum I II III

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][JavaScript]Combination Sum II

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 t

LeetCode 040 Combination Sum II

题目要求: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 (includ

Combination Sum 和Combination Sum II

这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在每个数有k +1中可能,k = target / i. 所以就是把简单的if else 分支变成for循环: vector<vector<int> > combHelper(vector<int>::iterator first, vector<int>::it

Combination Sum II leetcode java

题目: 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

040 Combination Sum II

040 Combination Sum II 这道题是 039 Combination Sum 的延伸, 稍作改动就好 class Solution: # @param {integer[]} candidates # @param {integer} target # @return {integer[][]} def combinationSum2(self, candidates, target): candidates.sort() ans = [] self.help(candidat