LeetCode 39. Combination Sum 组合总和 (C++/Java)
题目:
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]
]
分析:
给定一个无重复元素的数组,要求用数组中的元素构成组合(可以重复),使元素和与target相同,且最后答案中不能有重复的组合。
利用深度优先搜索,在搜索的时候要传入一个索引,递归搜素时,从当前索引处向后搜索元素,防止最后有重复的元素组合,同时每搜索到一个元素,使当前目标修改为target-元素,当目标为0时,代表我们找到了一个解,加入到结果集合中。
可以先将所有元素由小到大排序,当搜索过程中当前元素大于目标值,此时后面的所有元素都不符合要求,提前退出循环。
程序:
C++
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> curr;
sort(candidates.begin(), candidates.end());
dfs(res, curr, target, 0, candidates);
return res;
}
void dfs(vector<vector<int>>& res, vector<int> curr, int target, int index, vector<int>& candidates){
if(target == 0){
res.push_back(curr);
return;
}
for(int i = index; i < candidates.size(); ++i){
if(candidates[i] > target)
break;
//continue;
curr.push_back(candidates[i]);
dfs(res, curr, target-candidates[i], i, candidates);
curr.pop_back();
}
}
};
Java
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new LinkedList<>();
LinkedList<Integer> curr = new LinkedList<>();
Arrays.sort(candidates);
dfs(res, curr, target, 0, candidates);
return res;
}
private void dfs(List<List<Integer>> res, LinkedList<Integer> curr, int target, int index, int[] candidates){
if(target == 0){
res.add(new LinkedList<>(curr));
return;
}
for(int i = index; i < candidates.length; ++i){
if(candidates[i] > target)
break;
curr.addLast(candidates[i]);
dfs(res, curr, target-candidates[i], i, candidates);
curr.removeLast();
}
}
}
LeetCode 39. Combination Sum 组合总和 (C++/Java)的更多相关文章
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- 【LeetCode】Combination Sum(组合总和)
这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...
- [leetcode]39. Combination Sum组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- [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 (C) (without duplicates) and a target number (T), find all unique c ...
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所 ...
- leetcode 39 Combination Sum --- java
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Java [Leetcode 39]Combination Sum
题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 039 Combination Sum 组合总和
给一组候选数字(C)(没有重复)并给一个目标数字(T),找出 C 中所有唯一的组合使得它们的和为 T.可以从 C 无限次数中选择相同的数字.说明: 所有数字(包括目标)都是正整数. 解集合 ...
随机推荐
- 使用electron的demo时遇到的错误
使用electron的demo时的错误 Electron | Build cross-platform desktop apps with JavaScript, HTML, and CSS. (el ...
- ESXI 6.5 零基础从安装到批量生成/管理虚拟机简易教程
制造U盘安装盘 1 先提前下载好,ESXI 6.5 ISO文件. 2 下载制作U盘安装工具,RUFUS. Rufus非常小巧的绿色EXE文件,默认配置选中ISO文件就可以,点击开始,就自动制作,非常方 ...
- 技术解读:Dragonfly 基于 P2P 的智能镜像加速系统 | 龙蜥技术
简介: 结合 Dragonfly 子项目 Nydus 进行按需加载可以最大限度提升镜像下载速度. 编者按:上世纪末期,基于 C/S 模式的思想,人们发展了 HTTP . FTP 等应用层协议.然而 C ...
- MaxCompute管家详解--管家助力,轻松玩转MaxCompute
精彩视频回顾请点击:MaxCompute管家详解以下是直播内容精华整理,主要包括以下四个方面:1.背景速览:2.功能介绍:3.案例讲解:4.新功能预告. 一.背景速览 MaxCompute(原ODPS ...
- KubeCon 2020 演讲集锦|《阿里巴巴云原生技术与实践 13 讲》开放下载
2020 年 7 月 30 日至 8 月 1 日,由 Cloud Native Computing Foundation (CNCF) 主办的云原生技术大会 Cloud Native + Open S ...
- 企业版Spark Databricks + 企业版Kafka Confluent 联合高效挖掘数据价值
简介:本文介绍了如何使用阿里云的Confluent Cloud和Databricks构建数据流和LakeHouse,并介绍了如何使用Databricks提供的能力来挖掘数据价值,使用Spark ML ...
- dotnet 使用 ConfigureAwait.Fody 库设置默认的 await 同步上下文切换配置
在 dotnet 里面,使用 await 进行异步逻辑,默认是会尝试切换回调用 await 的线程同步上下文.这个机制对于大多数的上层应用来说都是符合逻辑且方便的逻辑,例如对于带 UI 线程的 WPF ...
- WPF 给类库设置设计时使用的资源字典
在开发 WPF 类库时,由于类库里面没有存在 App.xaml.cs 文件,而在对单个 XAML 进行开发时,设计器将会因为找不到资源文件的存在,而拿不到资源.本文告诉大家简单的方法,给设计器设置仅在 ...
- 3. ETCD数据备份与恢复
首先为运行在https://127.0.0.1:2379 上的现有etcd实例创建快照并将快照保存到 /srv/data/etcd-snapshot.db. 注:为给定实例创建快照预计能在几秒钟内完成 ...
- Docker的基本命令
1.docker使用的优点 1.更快速的交付和部署 对于开发和运维人员来说,最希望的是保持所有环境一致,这样不会导致,开发在自己的环境里程序运行正常而运维跑的服务器环境里就不正常:对于运维来说,可以使 ...