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. SQL 判断 ‘表,存储过程,函数 ...’ 已是否存在

    下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助.库是否存在if exists(select * from master..sys ...

  2. 在Linux中设置自启动服务或程序

    三种方法: 1.基于linux的system V机制,其中有个运行级别和链接软连接指向服务脚本的机制. 服务脚本一般处于/etc/init.d/目录下, 而运行级别制定的默认执行脚本在/etc/rc. ...

  3. 采用Jenkins搭建持续集成环境

    Jenkins介绍 Jenkins是一个CI工具.它可以根据设定持续定期编译,运行相应代码:运行UT或集成测试:将运行结果发送至邮件,或展示成报告... 这样做的最终目的是: 让项目保持健康的状态.如 ...

  4. JFrame

    import java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyDemo extends JFram ...

  5. CodeForces 710A King Moves

    简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...

  6. [UWP小白日记-6]页面跳转过度动画

    前言 在学习中发现页面导航默认是没有过度动画的,直接就导航过去太粗暴了( ̄へ ̄),于是打算上动画结果不言而喻自己进了坑完全不懂动画,然后就是各种疯狂(´・_・`)的搜索资料看了后终于有点头绪. 再后来 ...

  7. 【1】ShopNC 模仿笔记(一)

    不断学习,人生将会成功. 1. 序 一直想模仿一个整套的商城, 今天在shopNC 里面看到了想要的一切, 所以把一些组件, 命名规范等记录下来, 提高以后的开发效率. 官方网站 PC : shopN ...

  8. reposync 同步yum仓库

    reposync 同步远程yum仓库到本地 OPTIONS       -h, --help              显示帮助 -c CONFIG, --config=CONFIG          ...

  9. 学习【Web前端开发修炼之道】总结

    网页布局 1.申明文档模式 2.设置css标签重置,避免各浏览器解析不同. 3.网页css模块划分,base.css,common.css,page.css 4.低权重原则---避免滥用子选择器 cs ...

  10. Pthon修炼5

    函数    一.自定义函数 def:表示函数的关键字 函数名:函数的名称 函数体:函数中进行一系列的逻辑计算 参数:为函数体提供数据 返回值:执行完函数return 一个值 例:def count(x ...