【题目】

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. 复旦大学2017--2018学年第二学期(17级)高等代数II期末考试第六大题解答

    六.(本题10分)   设 $A$ 为 $n$ 阶幂零阵 (即存在正整数 $k$, 使得 $A^k=0$), 证明: $e^A$ 与 $I_n+A$ 相似. 证明  由 $A$ 是幂零阵可知, $A$ ...

  2. Python 调用 C# dll库最简方法

    1.为什么要跨平台编程?双平台编程或多平台编程,只是为提供更好开发更兼容的解决方案的一种手段,编程时服务于产品和客户的,也是因地制宜. 先安装python所需的库clr ,我这里已经安装了,可以去对应 ...

  3. 浅谈JavaScript的函数的call以及apply

    我爱撸码,撸码使我感到快乐!大家好,我是Counter.今天就来谈谈js函数的call以及apply,具体以代码举例来讲解吧,例如有函数: function func(a, b) { return a ...

  4. Java基础学习-关键字的概述和特点以及常量的概述和分类

    1.关键字概述     -被Java语言赋予特定含义的单词 2.关键字的特点     -组成关键字的字母全部小写     -常用的代码编辑器,针对关键字有特殊的颜色标记,非常直观,所以我们不需要死记硬 ...

  5. javaScript数据类型的一些小细节

    JavaScript的数据类型有哪些就不说了,没必要. 由于JavaScript是一个弱类型的语言,就是定义的时候不需要定义数据类型,数据类型自动根据变量值来确定.而在JavaScript里面,数据类 ...

  6. 意外get接近完美的黑苹果 (UEFI + GPT)

    本人大学生一枚,对于高大上的 MAC OS 只能是摸摸口袋 咽咽口水啦.听说黑苹果,就是安装在普通的 pc 上的 MAC系统,那么对应的苹果电脑上的 MAC OS 系统就为白苹果了. 个人也想啃一口黑 ...

  7. LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.

    NormalSubmission=analysis.Count(x=>x.FinishTime<= endTime.AddDays(1))报错linq不能识别 => var endT ...

  8. vue中import引入模块路径中@符号是什么意思

    在编写vue文件中引入模块 import model from "@/common/model"; 这里路径前面的“@”符号表示什么意思? resolve: { // 自动补全的扩 ...

  9. Manjaro安装后,应该做的操作,仅作为自己备份使用,如有参考不懂,请留言咨询,或Q609916691

    家目录下,通用文件夹名称中英文互转: --(1)中文->英文 export LANG=en_US.UTF-8 xdg-user-dirs-update --force --(2)英文->中 ...

  10. 【js】【图片瀑布流】js瀑布流显示图片20180315

    js实现把图片用瀑布流显示,只需要“jquery-1.11.2.min.js”. js: //瀑布流显示图片 var WaterfallImg = { option: { maxWidth: 850, ...