【题目】

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

    • All numbers (including target) will be positive integers.
    • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]

Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

【思路】

回溯,不同在可以重复使用当前元素。相关题目

1、[Leetcode 78]求子集 Subset https://www.cnblogs.com/inku/p/9976049.html

2、[Leetcode 90]求含有重复数的子集 Subset II https://www.cnblogs.com/inku/p/9976099.html

3、讲解在这: [Leetcode 216]求给定和的数集合 Combination Sum III

4、[Leetcode 39]组合数的和Combination Sum

【代码】

通俗版,重点在flag=i。

class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> ans=new ArrayList<>();
List<Integer> tmp=new ArrayList<>();
fun(ans,tmp,candidates,0,target);
return ans;
}
public void fun(List<List<Integer>> ans,List<Integer> tmp,int[] data,int flag,int aim){
if(aim<0)return;
else if(aim==0)
ans.add(new ArrayList<>(tmp));
else{
for(int i=flag;i<data.length;i++){
tmp.add(data[i]);
fun(ans,tmp,data,i,aim-data[i]);
tmp.remove(tmp.size()-1);
}
}
}
}

改进,ans设为全局变量,两个判断合并成一次

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

[Leetcode 39]组合数的和Combination Sum的更多相关文章

  1. [Leetcode 40]组合数和II Combination Sum II

    [题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...

  2. LeetCode 39. 组合总和(Combination Sum)

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

  3. Leetcode之回溯法专题-39. 组合总数(Combination Sum)

    Leetcode之回溯法专题-39. 组合总数(Combination Sum) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  4. 【Leetcode】【Medium】Combination Sum II

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

  5. 【leetcode刷题笔记】Combination Sum II

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

  6. 【LeetCode每天一题】Combination Sum II(组合和II)

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  7. 【Leetcode】【Medium】Combination Sum

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

  8. 【leetcode刷题笔记】Combination Sum

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

  9. 【LeetCode每天一题】Combination Sum(组合和)

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

随机推荐

  1. MySql 查询表结构信息

    select Column_name as 列名,is_nullable as 是否可为空,data_type as 数据类型,column_default as 默认值,case when colu ...

  2. 【做题】ZJOI2017仙人掌——组合计数

    原文链接 https://www.cnblogs.com/cly-none/p/ZJOI2017cactus.html 给出一个\(n\)个点\(m\)条边的无向连通图,求有多少种加边方案,使得加完后 ...

  3. 6、Docker存储卷

    Why Data Volumes?  来自马哥教育 Data volumes Volume types  绑定挂载卷:在宿主机和容器上各指明绑定路径才能进行绑定. docker管理卷:在容器内指定挂载 ...

  4. python-request-各方法使用及格式

    Request库方法介绍 方法 说明 requests.request() 构造一个请求,支撑一下各方法的基础方法  requests.get()  获取HTML网页的主要方法,对应于HTTP的GET ...

  5. 树莓派3代刷ubuntu mate在命令行下配置wifi不能连接的一个诡异的bug的解决

    家里路由器不在自己卧室,用树莓派考虑用wifi,之前用Raspberry官方系统,按照教程写的wpa.conf可以连接wifi,后来重新刷ubuntu mate 16.04就不好用了 各种找原因,后来 ...

  6. Python之Beautiful Soup的用法

    1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...

  7. linux 内存-文档学习

    ptmalloc http://www.malloc.de/en/ tcmalloc https://github.com/gperftools/gperftools jcmalloc http:// ...

  8. 20175317 《Java程序设计》第八周学习总结

    20175317 <Java程序设计>第八周学习总结 教材学习内容总结 第八周我学习了教材第十五章的内容,认识了什么是泛型与集合框架,具体内容如下: 泛型 1. 如何声明泛型类 2. 如何 ...

  9. Activex、OLE、COM、OCX、DLL之间区别、联系[转]

    转自:http://baike.baidu.com/view/393671.htm 创建COM:http://blog.csdn.net/henry000/article/details/700839 ...

  10. idea 配置git

    1.注册https://github.com 2. 3.填入信息完成