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. Oracle11g安装与卸载教程

    1.1,前言: 电脑太卡,鄙人穷屌丝啊,没钱买新电脑,想想周六日还要耍游戏就给电脑重做了个系统,糟糕的是电脑上的各种环境,工具都需要重新装一边,包括oracle数据库- -,依稀记得昨天装了一上午的数 ...

  2. Cisco无线控制器配置

    一 组网图 System Name [Cisco_01::] ( characters max):wlc- //输入设备名称 Would you like to terminate autoinsta ...

  3. 高级java面试宝典

    1.spring事物的配置 spring事物分为俩种,一种是注解事物,一种是AOP事物注解事物的配置: 事物的隔离级别,事物的传播性,事物的超时回滚,哪些异常回滚,哪些不回滚,有默认的回滚规则注解事物 ...

  4. python3 __mian和__name__的区别

    1.新建 test.py 模块: def GetModuleName(): print('__name__ = ', __name__) def PrintName(): print('PrintNa ...

  5. js匿名函数确实是个好东西

    <body onload="alert('http://www.baidu.com/');"> <script type="text/javascrip ...

  6. swift 第四课 随意 设置button 图片和文字 位置

    项目中经常遇到按钮改变文字和图片位置的情况,所以尝试写一个 button 的分类: 参照连接 http://blog.csdn.net/dfqin/article/details/37813591 i ...

  7. 从MOV PC,PC;(或者ADDPC,PC,#4 )看ARM的三级流水线过程

    3级流水线如上图所示(PC为程序计数器),流水线使用3个阶段,因此指令分3个阶段执行. ⑴ 取指从存储器装载一条指令 ⑵ 译码识别将要被执行的指令 ⑶ 执行处理指令并将结果写会寄存器 以前学过的51单 ...

  8. Class WriteGroupAttribute

    [WriteGroup]可以排除在创建已声明写入同一组件的查询时未知的组件. 这允许安全地扩展组件系统而无需编辑先前存在的系统. 目标是为期望将数据从一组组件(输入)转换为另一组(输出[s])的系统能 ...

  9. java新特性stream

    java新特性stream,也称为流式编程. 在学习stream之前先了解一下java内置的四大函数 第一种函数式函数,后面是lambda表达式写法 /*Function<String,Inte ...

  10. curl安装问题--liburl3找不到

    问题: 大概就是,liburl3依赖没找到,或版本不对. 解决办法: 我们可以使用purge重新安装之. sudo apt-get purge libcurl3-gnutls sudo apt ins ...