import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Given a set of candidate numbers (C) (without duplicates) and a target number (T),
* find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]
回溯法,每次递归传入的目标值是当前目标值减去此次添加的值,递归函数每次先检测传入的目标值是否是0
如果是就添加到list中,由于没有负数存在,直接return就行。如果不是,那就遍历数组,要设定一个index记录当前遍历到
那个数了,递归中的遍历都是从index开始,这样可以避免重复数组的出现。当现在遍历到的数比当前目标值小就递归,
每次递归结束都要回溯(list删除上一个数);如果比目标值大了,由于数组已经排序,所以可以直接break。
*/
public class Q39CombinationSum {
public static void main(String[] args) {
int[] nums = new int[]{41,24,30,47,40,27,21,20,46,48,23,44,25,49,35,42,36,28,33,32,29,22,37,34,26,45};
System.out.println(combinationSum(nums,53));
}
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<>();
backTracking(result,candidates,new ArrayList<>(),target,0);
return result;
}
public static void backTracking(List<List<Integer>> result,int[] candidates,
List<Integer> cur,int left,int index)
{
if(left == 0)
{
result.add(new ArrayList<>(cur));
return;
}
for(int i = index;i < candidates.length;i++)
{
if (candidates[i] <= left)
{
cur.add(candidates[i]);
backTracking(result,candidates,cur,left-candidates[i],i);
cur.remove(cur.size()-1);
}
else
break;
} } }

[leetcode]39combinationsum回溯法找几个数的和为目标值的更多相关文章

  1. [Leetcode] Backtracking回溯法解题思路

    碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大 ...

  2. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

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

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

  4. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

  5. Leetcode之回溯法专题-79. 单词搜索(Word Search)

    Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...

  6. Leetcode之回溯法专题-77. 组合(Combinations)

    Leetcode之回溯法专题-77. 组合(Combinations)   给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输 ...

  7. Leetcode之回溯法专题-51. N皇后(N-Queens)

    Leetcode之回溯法专题-51. N皇后(N-Queens) n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给 ...

  8. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  9. Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)

    Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...

随机推荐

  1. fist-第七天冲刺随笔

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...

  2. android studio很好用的插件

  3. MySQL慢查询日志(SLOW LOG)

    慢查询日志可以帮助DBA或开发人员定位可能存在问题的SQL语句,从而进行优化. 如何开启 默认情况下,MySQL是不开启慢查询日志的.可以通过以下命令查看是否开启: mysql> SHOW VA ...

  4. day6(celery配置与基本使用)

    1.celery配置与基本使用 1.1 安装celery pip install celery @ https://github.com/celery/celery/tarball/master 1. ...

  5. 第7.13节 案例详解:Python类变量

    第7.13节 案例详解:Python类变量 上节介绍了类变量的定义和使用方法,并举例进行了说明.本节将通过一个更完整的例子来说明. 一.    定义函数dirp def dirp(iter): ret ...

  6. 第14.7节 Python模拟浏览器访问实现http报文体压缩传输

    一. 引言 在<第14.6节 Python模拟浏览器访问网页的实现代码>介绍了使用urllib包的request模块访问网页的方法.但上节特别说明http报文头Accept-Encodin ...

  7. PyQt(Python+Qt)学习随笔:视图中的dragDropMode属性对dragEnabled和acceptDrops属性的影响

    老猿Python博文目录 老猿Python博客地址 在<PyQt(Python+Qt)学习随笔:QAbstractItemView的dragEnabled和dragDropMode属性的关系&g ...

  8. 面试阿里,字节,腾讯90%被问到的springboot常见面试题,这次给你讲的明明白白!

    1.什么是Spring Boot? 多年来,随着新功能的增加,spring变得越来越复杂.只需访问https://spring.io/projects页面,我们就会看到可以在我们的应用程序中使用的所有 ...

  9. Outlook会议中人员窗格消失了

    Outlook会议中人员窗格消失了,如何恢复: 在[文件]-[信息]-[管理COM加载项] 启用Outlook social conector, 对启用,一直启用,再重启OUTLOOK即可.

  10. 大白话详解大数据hive知识点,老刘真的很用心(2)

    前言:老刘不敢说写的有多好,但敢保证尽量用大白话把自己复习的内容详细解释出来,拒绝资料上的生搬硬套,做到有自己的了解! 1. hive知识点(2) 第12点:hive分桶表 hive知识点主要偏实践, ...