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]

Subscribe to see which companies asked this question

【题目解析】

给定一个候选数字序列一个目标值,找到所有的和等于目标值的候选数字组合。同一个候选数字可以出现多次。

1. 所有的数字是正数;

2. 组合中的数字降序排列;

3. 结果中不能存在相同的组合;

【思路】

首先我们从一个简单的例子分析一下这样组合生成的过程:

[1,2,3] target = 7

我们从[1,2,3]生成所有的和等于7的数字组合,可以分为以1开头的有:

[1,1,1,1,1,1,1],[1,1,1,1,1,2],[1,1,1,1,3],[1,1,1,2,2],[1,1,2,3],[1,2,2,2],[1,3,3]

以2开头的:

[2,2,3]

上面这几个组合是满足所有条件的组合。生成过程可以这样描述:

1. 给定一个升序排序的数组;

2. 从数组头部向后遍历,如果遇到一个比目标值小的数n,我们找到目标值为target - n的所有组合,如果找到这样的组合,那么我们把n合并到每一个组合里;

3. 如果遇到一个值m = target 则新建一个List,添加m后返回;

4. 如果遇到一个值m > target 则终结遍历,因为之后的数字肯定比target还大;

很明显这是一个递归的过程,在这个过程中我们首先对候选数组进行了排序,这是为什么呢? 因为在递归的过程中,如果一个数字n小于target,那么在递归求解target - n时我们可以确定一个从候选数组重新开始的下标,这个下标就是当前数字的下标。

【java代码】

 public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates); //升序排序
return combination(candidates, target, 0);
} public List<List<Integer>> combination(int[] candidates, int target, int start) {
List<List<Integer>> list = new ArrayList<>();
if(candidates == null || candidates.length == 0) return list; for(int i = start; i < candidates.length; i++){
if(candidates[i] < target){
List<List<Integer>> tlist = combination(candidates, target - candidates[i], i);
if(tlist.size() > 0){
for(List<Integer> alist : tlist){
alist.add(0, candidates[i]);
}
list.addAll(tlist);
}
}
else if(candidates[i] == target){
List<Integer> tlist = new LinkedList<>();
tlist.add(target);
list.add(tlist);
}
else break;
}
return list;
}
}

LeetCode OJ 39. Combination Sum的更多相关文章

  1. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  2. LeetCode OJ 40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  4. 【LeetCode】39. Combination Sum (2 solutions)

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  5. 【一天一道LeetCode】#39. Combination Sum

    一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...

  6. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  7. LeetCode OJ:Combination Sum II (组合之和 II)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. LeetCode OJ:Combination Sum (组合之和)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. LeetCode OJ:Combination Sum III(组合之和III)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. LeetCode 392. Is Subsequence

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  2. Cracking the Coding Interview 第二章

    2.2 链表中倒数第k个结点 输入一个链表,输出该链表中倒数第k个结点. 思路:快慢指针(error: 判断是否有可行解,否则返回null, while, if 后加空格) /* public cla ...

  3. 今天学习的裸板驱动之存储控制器心得(初始化SDRAM)

    CPU只管操作地址,而有些地址代表的是某些存储设备. 但是操作这些存储设备需要很多东西,比如需要制定bank,行/列地址等.所以就有了存储管理器,用来处理这种CPU操作的地址和存储设备间的转换. (1 ...

  4. ubuntu 14.04中文分词 结巴分词

    在自然语言处理中,很常见的是要对文本数据进行分词处理.博主是代码小白,目前只是用python作为数据预处理的工具,而按照结巴中文分词的导语:做最好的python中文分词组件“jieba”.因而博主也就 ...

  5. 【5】图解HTTP 笔记

    坚持.聪明.不畏困难,我将取得最后的胜利. 第一章 了解 Web 以网络基础 1. HTTP ( HyperText Transfer Protocol ): 超文本传输协议. 2. 通讯方式: 3. ...

  6. Jquery几秒自动跳转

    $(document).ready(function() { function jump(count) { window.setTimeout(function(){ count--; if(coun ...

  7. 【NOIP2006提高组】能量项链

    说好的好好写人话的题解 嗯很多题解都说过这是一个石子合并的模型它也确实就是一个石子合并的模型.然而就算这样我也不会写最后仍然写了个记忆化搜索 首先我们不论环状,就直接一条链型,当只剩下两个珠子的时候, ...

  8. [DP优化方法之斜率DP]

    什么是斜率dp呢 大概就把一些单调的分组问题 从O(N^2)降到O(N) 具体的话我就不多说了 看论文: http://www.cnblogs.com/ka200812/archive/2012/08 ...

  9. JDOM与DOM主要有两方面不同

    我这丝毫没有吐槽的意思哟,只是想说作为一个合格的程序员大家最起码需要做到思维严谨这点,在有能力的情况下对用户体验能提点建议最好.自己写的代码一定要经过严格测试再交付,不要指望测试人员帮你测试再去修改, ...

  10. ORACLE ORDER BY用法总结

    order by后面的形式却比较新颖(对于我来说哦),以前从来没看过这种用法,就想记下来,正好总结一下ORDER BY的知识. 1.ORDER BY 中关于NULL的处理 缺省处理,Oracle在Or ...