`

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 (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.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]

有关组合求和的问题,我们采用回溯法,从一条路走下去,如果找到结果就记录下来,如果走到尽头没找到结果就开始回溯,因为题目要求每个数字可以使用多次,因此我们往下寻找答案的起始点都是从当前元素开始。其次,题目要求结果集为非降序列,所以我们要先将数组排序在做处理。代码如下:
public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        LinkedList<Integer> list = new LinkedList<Integer>();
        List<List<Integer>> llist = new LinkedList<List<Integer>>();
        if(candidates == null || candidates.length == 0) return llist;
        java.util.Arrays.sort(candidates);
        getCombination(0, target, candidates, list, llist);
        return llist;
    }
    public void getCombination(int start, int target, int[] candidates, LinkedList<Integer> list, 
    List<List<Integer>> llist) {
        for(int i = start; i < candidates.length; i++) {
            if(target > candidates[i]) {
                list.add(candidates[i]);
                getCombination(i, target - candidates[i], candidates, list, llist);
                list.removeLast();
            } else if(target == candidates[i]) {
                list.add(candidates[i]);
                if(!llist.contains(list))
                    llist.add(new LinkedList<Integer>(list));
                list.removeLast();
            } else {
                return;
            }
        }
    }
}
0
0
分享到:
评论

相关推荐

    java-leetcode题解之Combination Sum.java

    在这个Java代码中,我们首先定义了一个名为`Solution`的类,并在其中定义了一个名为`combinationSum`的方法。该方法接收两个参数:一个整数数组`candidates`和一个整数`target`。`candidates`数组中的元素可以无限次...

    java-leetcode题解之Combination Sum II.java

    虽然问题表面上看起来和Combination Sum I类似,但Combination Sum II由于限制了数字的使用次数,使得问题复杂度有所提高。 在解决Combination Sum II时,首先需要对数组进行排序,排序的目的是为了在后续的递归...

    java-leetcode题解之Combination Sum III.java

    其中,“Combination Sum III”是其中的一道典型的回溯算法题。 “Combination Sum III”题目要求给定一个范围,找出所有相加之和为特定值的k个数的组合。在这个问题中,所有数字必须是唯一的,且数字范围限制在1到...

    java-leetcode题解之Combination Sum IV.java

    public int combinationSum4(int[] nums, int target) { int[] dp = new int[target + 1]; dp[0] = 1; for (int i = 1; i ; i++) { for (int num : nums) { if (i &gt;= num) { dp[i] += dp[i - num]; } } } ...

    js-leetcode题解之39-combination-sum.js

    在JavaScript编程中,解决算法...function combinationSum(candidates, target) { let result = []; let combination = []; // 递归函数,用于回溯搜索 function backtrack(remain, combo, start) { if (remain

    python-leetcode题解之216-Combination-Sum-III.py

    特别是算法题目,如216题——Combination Sum III,对于锻炼编程者的算法思维和代码实现能力有着重要作用。 Combination Sum III这道题目要求解的是找出所有和为特定值的组合,其中每个数字仅能使用一次,这些数字...

    C语言-leetcode题解之39-combination-sum.c

    LeetCode题解之39-Combination Sum是一个典型的C语言算法问题,主要考察候选者对回溯算法的理解和实现能力。该问题的具体内容是找出所有相加之和为给定目标数target的组合,且每组中每个数字可以重复使用多次。解决...

    C语言-leetcode题解之40-combination-sum-ii.c

    文件标题"C语言-leetcode题解之40-combination-sum-ii.c"暗示了本文的内容是围绕LeetCode上的第40题"Combination Sum II"编写的题解,并且使用C语言来实现解决方案。第40题是组合总和问题的一个变种,要求在一组数字...

    js-leetcode题解之40-combination-sum-II.js

    首先,“js-leetcode题解之40-combination-sum-II.js”是一份JavaScript编程语言实现的LeetCode算法题解。LeetCode是一个在线编程平台,主要提供算法训练和面试准备题目,题目涵盖从初级到高级的不同难度级别。这道...

    Coding Interview In Java

    239 Combination Sum II 579 240 Combination Sum III 581 241 Combinations 583 242 Letter Combinations of a Phone Number 587 243 Restore IP Addresses 589 244 Reverse Integer 591 245 Palindrome Number 593

    手稿_V1.088

    代码首先定义了一个名为`Solution`的类,其中有两个方法:`bit1Count`和`combinationSum3`。`bit1Count`方法用于计算一个无符号整数中二进制形式的1的个数,而`combinationSum3`是解决该问题的主要方法。 在`bit1...

    python-leetcode面试题解之第216题组合总和III-题解.zip

    def combinationSum3(self, k: int, n: int, m: int, current_sum=0, current_combination=[], combinations=[]): if len(current_combination) == m and current_sum == k: combinations.append(current_...

    几百种Python 算法集合

    Combination Sum组合总和 Crossword Puzzle Solver 填字游戏求解器 Generate Parentheses 生成括号 Hamiltonian Cycle 哈密顿循环 Knight Tour骑士之旅 Match Word Pattern 匹配单词模式 Minimax极大 极小 N QueensN...

    组合总和(java代码).docx

    public static List&lt;List&lt;Integer&gt;&gt; combinationSum(int[] candidates, int target) { List&lt;List&lt;Integer&gt;&gt; result = new ArrayList(); List&lt;Integer&gt; combination = new ArrayList(); Arrays.sort(candidates);...

    c++-c++编程基础之leetcode题解第40题组合总和II.zip

    在主函数`combinationSum2`中,我们先对输入的`candidates`进行排序,然后调用`backtrack`函数开始搜索过程。最终返回的结果集`res`包含了所有满足条件的组合。 通过以上代码,我们可以解决LeetCode第40题...

    手稿_V1.015

    最后,`combinationSum2`是对外的公共接口,它初始化结果向量`vvi`,并调用`helper`函数开始搜索过程。 总的来说,这个手稿展示了如何使用C++实现一个基于回溯和剪枝的解决方案,来解决组合问题。通过理解这个示例...

    c++-c++编程基础之leetcode题解第39题组合总和.zip

    vector&lt;vector&lt;int&gt;&gt; combinationSum(vector&lt;int&gt;& candidates, int target) { vector&lt;vector&lt;int&gt;&gt; result; vector&lt;int&gt; current; backtrack(candidates, target, result, current, 0); return result; } void...

    java-leetcode题解之039-Combination-Sum

    java入门 java_leetcode题解之039_Combination_Sum

Global site tag (gtag.js) - Google Analytics