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. bypass disable_function

    windows 1.com组件绕过 <?php$command=$_POST['a'];$wsh = new COM('WScript.shell'); // 生成一个COM对象 Shell.A ...

  2. ERP费用报销操作与设计--开源软件诞生31

    赤龙ERP费用报销讲解--第31篇 用日志记录"开源软件"的诞生 [进入地址 点亮星星]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/redra ...

  3. 第15.47节、PyQt显示部件:QGraphicsView图形视图和QGraphicsScene图形场景简介及应用案例

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 Designer中的Graphics V ...

  4. 使用PyQt(Python+Qt)+动态编译36行代码实现的计算器

    PyQt是基于跨平台的图形界面C++开发工具Qt加Python包装的一个GPL软件(GPL是GNU General Public License的缩写,是GNU通用公共授权非正式的中文翻译),Qt基于 ...

  5. System.out.println();快捷键

    不同开发工具的输出快捷键是不一样的-System.out.println(); eclipse&my eclipse中是syso +(alt+/) idea 是sout +tab 或者sout ...

  6. 影评网站Alpha版本-测试与发布

    影评网站Alpha版本-测试与发布 项目发布地址: http://120.78.161.21:8080/zhiying/ (建议使用Chrome或火狐浏览器打开,其他浏览器可能加载失败 一.Alpha ...

  7. css之div中纯文字单行和多行垂直居中

    先上效果图 <html lang="en"> <head> <meta charset="UTF-8"> <meta ...

  8. rsync 参数说明及使用参数笔记好文摘抄

    一.前言 最近发现rsync挺好用的--不过参数有点多,所以这儿写一篇给自己以后要用的时候做个参考. 二.参数说明 这儿全是我翻资料连蒙带猜(有些实在是不好解释)翻译出来的,请各位转载的留个名啊,虽然 ...

  9. DBeaver连接MySQ报错

    遇错情况:第一次使用DBaver连接MySQL遇到以下问题: 报错信息:Public Key Retrieval is not allowed 截图如下: 解决方案步骤: 一.已有连接的情况:F4或者 ...

  10. PHP 读取XML大文件格式并将其存入数据库中

    <?php     $xml = new XMLReader(); $xmlfile="./full_database.xml";#文件路径   $xml->open( ...