[LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)
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 ☆☆☆(数组相加等于指定的数)的更多相关文章
- [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所 ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- leetcode 39 Combination Sum --- java
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode 39. Combination Sum (组合的和)
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
- Java [Leetcode 39]Combination Sum
题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [leetcode]39. Combination Sum组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
随机推荐
- mac下不允许安装除了app store之外的软件设置:
1.dock栏的系统偏好设置. 2.找到安全性与隐私 3.点击面板中的通用,在点击左小角的锁按钮, 4.选择任何来源,确定就可以了.[如果只有两个选项,而没有任何来源的话,打开终端,执行:sudo s ...
- 使用dd命令克隆整个Linux系统
参考:https://www.cnblogs.com/jikexianfeng/p/6103504.html 本次使用使用dd命令克隆整个Ubuntu系统 1,VM安装一台Ubuntu虚拟机 过程不详 ...
- Spring Aop(十二)——编程式的创建Aop代理之AspectjProxyFactory
转发地址:https://www.iteye.com/blog/elim-2397922 编程式的创建Aop代理之AspectjProxyFactory 之前已经介绍了一款编程式的创建Aop代理的工厂 ...
- vim实现批量注释和批量删除注释
批量注释 1.进入文档,vim test.txt 后,按住ctrl+v进入VISUAL BLOCK模式,上下选择需要注释的行 2.按大写键,再按i,或者直接按shift+i,进入INSERT模式,输入 ...
- 【VS开发】QueryPerformanceFrequency与QueryPerformanceCounter的使用
LARGE_INTEGER tima,timb; QueryPerformanceCounter(&tima); 在 Windows Server 2003 和 WindowsXP 中使用 Q ...
- mac go环境的安装和卸载
背景: go环境的安装和卸载, 之前安装过go1.12, 现在项目需要,要安装go1.13. 所以要做的是先卸载, 然后在安装 本文介绍以下几个问题 1. go环境的卸载 2. go环境的安装 3. ...
- 最新 小米java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.小米等10家互联网公司的校招Offer,因为某些自身原因最终选择了小米.6.7月主要是做系统复习.项目复盘.LeetCode ...
- Golang中string和[]byte的对比
golang string和[]byte的对比 为啥string和[]byte类型转换需要一定的代价? 为啥内置函数copy会有一种特殊情况copy(dst []byte, src string) i ...
- js穿梭框;将两个table中的数据选中移动
将table中选中的数据移动到右边: 点击一行中的任意一个位置,使其选中: 注:attr()和prop()都是jquery的方法: .attr() : 获取匹配的元素集合中的第一个元素的属性的值 或 ...
- [github] 关于华为鸿蒙OS
English Docs | 中文文档 | Türkçe Dökümanlar HarmonyOS Ⅰ. 鸿蒙系统简介 鸿蒙系统(HarmonyOS),是第一款基于微内核的全场景分布式OS,是华为自主 ...