https://leetcode.wang/leetCode-39-Combination-Sum.html

描述

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]
]

解析

回溯法

参考这里) ,就是先向前列举所有情况,得到一个解或者走不通的时候就回溯。和37题有异曲同工之处,也算是回溯法很典型的应用,直接看代码吧。

动态规划

看原文

代码

回溯法

public List<List<Integer>> combinationSum(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<>();
backtrack(list, new ArrayList<>(), nums, target, 0);
return list;
} private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){
if(remain < 0) return;
else if(remain == 0) list.add(new ArrayList<>(tempList));
else{
for(int i = start; i < nums.length; i++){
tempList.add(nums[i]);
backtrack(list, tempList, nums, remain - nums[i], i);
//找到了一个解或者 remain < 0 了,将当前数字移除,然后继续尝试
tempList.remove(tempList.size() - 1);
}
}
}

[LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)的更多相关文章

  1. [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...

  2. LeetCode 39 Combination Sum(满足求和等于target的所有组合)

    题目链接: https://leetcode.com/problems/combination-sum/?tab=Description   Problem: 给定数组并且给定一个target,求出所 ...

  3. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  4. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  5. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  6. leetcode 39 Combination Sum --- java

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  7. LeetCode 39. Combination Sum (组合的和)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  8. Java [Leetcode 39]Combination Sum

    题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in  ...

  9. [leetcode]39. Combination Sum组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

随机推荐

  1. linux 测试磁盘iops 方法详解

    一.FIO安装  wget http://brick.kernel.dk/snaps/fio-2.0.7.tar.gz  yum -y install libaio-devel  tar -zxvf ...

  2. Reveal v4(8796) 使用

    文件下载地址   http://xclient.info/s/reveal.html dmg安装成功后, pod install  植入项目. pod 'Reveal-SDK', '~> 4', ...

  3. Vue学习笔记(二)

    1.引入组件需要注意的事项说明和步骤: 第一步,引入对应的组件: import Hello2 from './components/Hello2' 第二步: 需要注册该组件才可以使用 <scri ...

  4. shell变量默认值

    ${var:-string},${var:+string},${var:=string},${var:?string} 1. ${var:-string}和${var:=string}:若变量var为 ...

  5. 撸一个vue的双向绑定

    1.前言 说起双向绑定可能大家都会说:Vue内部通过Object.defineProperty方法属性拦截的方式,把data对象里每个数据的读写转化成getter/setter,当数据变化时通知视图更 ...

  6. centos安装软件

    rpm指令, 该指令安装文件后缀.rpm的可执行程序 yum指令 安装软件源代码,后缀为 .tar.gz(用gzip压缩过的tar包) rpm rpm软件包格式为 (一)查询系统装已经安装的软件信息 ...

  7. C# 添加日志文件

    StreamWriter log_sw; // 新建文件 log_sw = File.AppendText(log_str); // 写入日志文件 log_sw.WriteLine(s + " ...

  8. 学习笔记:CentOS7学习之十九:Linux网络管理技术

    目录 学习笔记:CentOS7学习之十九:Linux网络管理技术 本文用于记录学习体会.心得,兼做笔记使用,方便以后复习总结.内容基本完全参考学神教育教材,图片大多取材自学神教育资料,在此非常感谢MK ...

  9. poj1753 (高斯消元法求异或方程组)

    题目链接:http://poj.org/problem?id=1753 题意:经典开关问题,和poj1222一样,进行两次高斯消元即可,只用初始化的时候改一下初始状态.可能存在无解或多解的情况,多解要 ...

  10. React Native 中 component 生命周期

    React Native 中 component 生命周期 转自 csdn 子墨博客  http://blog.csdn.net/ElinaVampire/article/details/518136 ...