给一组候选数字(C)(没有重复)并给一个目标数字(T),找出 C 中所有唯一的组合使得它们的和为 T。
可以从 C 无限次数中选择相同的数字。
说明:
    所有数字(包括目标)都是正整数。
    解集合中没有相同组合。
例如,给一个候选集合 [2, 3, 6, 7] 并且目标为 7,
一个解的集合为:
[
  [7],
  [2, 2, 3]
]

详见:https://leetcode.com/problems/combination-sum/description/

Java实现:

class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<Integer> out=new ArrayList<Integer>();
Arrays.sort(candidates);
helper(candidates,target,0,out,res);
return res;
}
private void helper(int[] candidates, int target,int start,List<Integer> out,List<List<Integer>> res){
if(target<0){
return;
}
if(target==0){
res.add(new ArrayList<Integer>(out));
}else{
for(int i=start;i<candidates.length;++i){
out.add(candidates[i]);
helper(candidates,target-candidates[i],i,out,res);
out.remove(out.size()-1);
}
}
}
}

参考:http://www.cnblogs.com/grandyang/p/4419259.html

039 Combination Sum 组合总和的更多相关文章

  1. 【LeetCode】Combination Sum(组合总和)

    这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...

  2. Leetcode39.Combination Sum组合总和

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

  3. [LeetCode] Combination Sum 组合之和

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

  4. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  5. LeetCode 039 Combination Sum

    题目要求:Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique c ...

  6. Java for LeetCode 039 Combination Sum

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

  7. [leetcode]39. Combination Sum组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  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】039. Combination Sum

    题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...

随机推荐

  1. listen and translation exercise 53

    It was hard work and there weren't any interesting things for him. You should be an expert with comp ...

  2. ACM学习历程——UVA540 Team Queue(队列,map:Hash)

    Description   Team Queue   Team Queue  Queues and Priority Queues are data structures which are know ...

  3. 打开关闭tomcat的目录浏览功能

    目录浏览功能 conf/web.xml中init-param中有对于listing的定义,设置为true即可实现tomcat的目录浏览: tomcat的管理用户设置 conf/tomcat-users ...

  4. CF285 E Positions in Permutations——“恰好->大于”的容斥和允许“随意放”的dp

    题目:http://codeforces.com/contest/285/problem/E 是2018.7.31的一场考试的题,当时没做出来. 题解:http://www.cnblogs.com/y ...

  5. POJ3264(线段树入门题)

    Balanced LineupCrawling in process... Crawling failed Time Limit:5000MS     Memory Limit:65536KB     ...

  6. ie下使用文本编辑器导致input文本框无法聚焦的问题原因

    最近公司的一个项目遇到一个如下问题: 描述:测试发现,每当我们在发布活动的页面发布完活动后,页面跳转到我的活动,然后再次回到发布活动页面,发现所有的input文本框都不能聚焦.然后再去看看其他页面(我 ...

  7. linux 中C语言便于调试的宏定义编写及 __FILE__,__FUNCTION__, __LINE__参数使用

    转自:http://blog.csdn.net/edonlii/article/details/8491342/ 在linux编程中,当文件数量变的众多之后,使用gdb调试就是一场灾难.因此在程序中加 ...

  8. MFC获取数据的方式

    假设输入框ID是:ID_NUMBER1,ID_NUMBER2,ID_NUMBER3. 获取数据的方式是: int number1,number2,number3; number1 = GetDlgIt ...

  9. SoapUI登录测试(2)-- 断言

    SoapUI登录测试(1)的结果为: 可以看到只有第2步是成功的,1.3的结果是unKnown,这里并没有对1.3两步添加断言,判断testCase中的这2步是否通过. 一.添加断言 1. /logi ...

  10. spring加载hibernate映射文件的几种方式

    1. 在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessi ...