【题目】

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. LGOJ P3834 【模板】可持久化线段树 1(主席树)

    代码 #include <cstdio> #include <iostream> #include <algorithm> using namespace std; ...

  2. python2 与 python3的区别

    python2 与 python3的区别 几乎所有的python2程序都需要一些修改才能正常的运行在python3的环境下.为了简化这个转换过程,Python3自带了一个2to3的实用脚本.这个脚本会 ...

  3. ASP.NET Core API 接收参数去掉烦人的 [FromBody]

    在测试ASP.NET Core API 项目的时候,发现后台接口参数为类型对象,对于PostMan和Ajax的Post方法传Json数据都获取不到相应的值,后来在类型参数前面加了一个[FromBody ...

  4. PostgreSQL 与 PostGIS

    PostgreSQL 是一种对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大.特性最丰富和最复杂的自由软件数据库系统.它起源于伯克利(BSD)的数据库研究计划,目前是最重要的开源数据库产 ...

  5. async/await 的使用

    async : 使用 async 修饰符可将方法.lambda 表达式或匿名方法指定为异步. 如果对方法或表达式使用此修饰符,则其称为异步方法 await: await 运算符应用于异步方法中的任务, ...

  6. 从svn到git开发转变

    前言:目前的公司的开发技术还是处于刀耕火种的年代,react,vue已经火到不行了,可是还在用jQuery一遍遍处理着dom.版本控制用的是svn,这里也不是说svn不好,在windows下svn的“ ...

  7. H5外包团队 H5开发微信APP的优势有哪些

    H5外包团队 H5开发微信APP的优势有哪些

  8. 简单理解epel源

    EPEL源-是什么全称   EPEL源   EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适 ...

  9. C#_计算目前时间到指定的周X、指定的时间X 还有多少秒

    比如:当前时间到下周二 05:00:00还剩下多少秒? /// <summary> /// 计算距离下一个 周XX XX时XX分XX秒,还剩下多少秒 /// </summary> ...

  10. 【转】HDMI之TMDS信号

    转自:https://blog.csdn.net/wangdapao12138/article/details/79935821 HDMI传输原理和DVI相同,由Silicon Image公司发明的T ...