【题目】

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. PL/SQL执行计划查看

    一.如何查看PLSQL的执行计划 在SQl Window窗口输入sql语句,然后按键"F5",就会进入执行计划查看界面. 二.界面说明 首先我们看第二行有几个属性可以选“Tree” ...

  2. Catalog of Patterns of Enterprise Application Architecture

    Catalog of Patterns of Enterprise Application Architecture Last Significant Update: January 2003 A s ...

  3. [524.A]2019-05-02(星期四)登顶梧桐山邀请

    *** 看房活动召集 ***五一期间天气炎热, 除了登山活动, 还将组织看房活动.拥有一套自己的住房是很多深圳人的梦想.政府十三五期间计划供应人才住房和保障性住房35万套, 与需求相比仍很少, 排队的 ...

  4. [ZOJ 4014] Pretty Matrix

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5742 AC代码: /* * 反思: * 1.遇到简单题别激动,先把它 ...

  5. DTS(待了解)

    DTS(待了解)  vs trasaction事务 脏数据 && 脏数据的清理 永远返回非空对象(忌:返回空值) 异常: invoker(trackTrace:debug.releas ...

  6. Java基本语法(一)

    1,Java中命名规则与规范 命名规则是我们必须遵守的约定: 1,Java中需要命名的地方(我们称之为标识符),可以26个英文字母(不区分大小写),0-9的数字,_和$等组成,不能包含特殊字符(#), ...

  7. hadoop配置项笔记 - hdfs

    hadoop版本:3.1.1 core-site.xml dfs.namenode.rpc-address 作用:rpc地址.我在使用distcp时显式使用到了. 默认值:port是8020 我的设置 ...

  8. 他山之石,calling by share——python中既不是传址也不是传值

    事情是这样的,Python里是传址还是传值令人疑惑,限于本人没有C基础,所以对大家的各类水平层次不一的解答难以确信. 第一个阶段: 在读<python基础教程第二版>的时候感到疑惑,然后群 ...

  9. 常见adb指令

    1. adb –-help 查看帮助文档 2. adb start-server 当adb没有启动或被手动杀掉时,可以使用该命令启动服务 3. adb kill-server 杀死adb服务 4. a ...

  10. 上海高校程序设计联赛 D-CSL的字符串 栈模拟

    题目链接:https://ac.nowcoder.com/acm/contest/551/D ASCII码表示的字符转换成整数实测不超过200(具体多少懒得查了) 分析:要求总的字典序最小,那就让最小 ...