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. jack语言编译器的实现过程

    目录: 1, 背景介绍

  2. labelme

    项目:https://github.com/wkentaro/labelme?tdsourcetag=s_pcqq_aiomsg 说明:https://www.bilibili.com/video/a ...

  3. CCIE总结:路由器、交换机

    bbs.spoto.net/forum--.html -----雏鹰部落 GNS3安装 .安装的所有目录不能使用中文 ISO如何操作 securecrt如何使用建立会话:之前总是连不上的原因是没有选 ...

  4. 继承System.Web.UI.Page的页面基类

    服务器端的page类      所有我们编写的页面都继承自page类,可见page类是非常重要的,page类提供了哪些功能,直接决定了我们的页面类可以继承什么功能,或者说,直接决定了我们的页面类功能的 ...

  5. dlopen, dlsym今天才刚知道干什么用的,羞死人了

    dlopen, dlsym今天才刚知道干什么用的,羞死人了

  6. Vue面试题总结——目录

    首先致敬所有积极分享自己的学习经验的程序猿.本文及其链接的绝大部分文章均属各个网站上面排名靠前,条理清晰的文章.考虑到如果只放链接可能会存在失效导致无法访问的问题,对应的复制粘贴了这些文章过来.对每一 ...

  7. Linux用户管理重要初始化目录login

    /etc/login.defs 配置文件 /etc/login.defs  文件是用来定义创建用户时需要的一些用户的配置信息.如创建用户时,是否需要家目录,UID和GID的范围,用户及密码的有效期限等 ...

  8. 日常工作问题解决:记一次centos7上的lvm表错误解决过程

    问题描述: 公司大数据hadoop2服务器采用电信云服务器,后来故障,电信恢复该服务器,需要重新部署程序,需要扩展lvm分区,但是使用pvsan命令发现有报错信息,需要解决以防重启后,因挂载问题,无法 ...

  9. Linux安装jemalloc笔记

    前言 最近研究一个工具库需要用 jemalloc 做内存分配器,但在 ubuntu 下安装过程中遇到很多问题,故记下安装过程的笔记,避免以后遇到在这上面浪费时间. 安装过程 环境:VMware Ubu ...

  10. Python基础总结之初步认识---class类(中)。第十四天开始(新手可相互督促)

    昨天简单的认识类怎么定义,什么是类,类如何调用.今天的笔记会大概补充一些内容,明天的笔记会细致讲解,加深个印象即可 今天我们在了解下:类的属性,类属性属于类也属于实例化对象.也就是说类的实例化对象可以 ...