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 无限次数中选择相同的数字.说明: 所有数字(包括目标)都是正整数. 解集合 ...
随机推荐
- 技术门槛高?来看 Intel 机密计算技术在龙蜥社区的实践 | 龙蜥技术
简介: 数据可用不可见是怎么做到的? 编者按:龙蜥社区云原生机密计算 SIG 定位于云原生机密计算底层基础设施,专注于机密计算底层技术.在阿里巴巴开源开放周中, 龙蜥社区机密计算 SIG Mainta ...
- 如何在 Anolis 8上部署 Nydus 镜像加速方案?
简介: 手把手教你在 Anolis OS 上部署 Nydus! 在上一篇文章中详细介绍Anolis OS 是首个原生支持镜像加速 Linux 内核,Nydus 镜像加速服务重新优化了现有的 OCIv1 ...
- 深入理解C++中的RVO
前言 考虑存在这样一个类如HeavyObject,其拷贝赋值操作比较耗时,通常你在使用函数返回这个类的一个对象时会习惯使用哪一种方式?或者会根据具体场景选择某一种方式? // style 1 Heav ...
- 从no-code到low-code:企业级hpaPaaS的未来
简介: 本文将简单谈一谈基于 no-code > low-code > pro-code 渐进式思路的研发体系. 引子 宜搭负责人骁勇给我举过一个例子,我们小时候逢年过节穿的衣服,都是去裁 ...
- 网易游戏基于 Flink 的流式 ETL 建设
简介: 网易游戏流式 ETL 建设实践及调优经验分享- 网易游戏资深开发工程师林小铂为大家带来网易游戏基于 Flink 的流式 ETL 建设的介绍.内容包括: 专用 ETL EntryX 通用 ETL ...
- Apache RocketMQ + Hudi 快速构建 Lakehouse
简介:基于RocketMQ和Hudi零代码构建Lakehouse架构,以及RocketMQ Connector & RocketMQ Stream助力ETL数据分析,为大家提供快速构建Lak ...
- [FAQ] Mac Mini 怎么让主机不休眠
Mac Mini 的防止休眠设置,在首选项,显示器里. 显示器里找到高级按钮. 然后有个开关是:显示器关闭时,防止自动进入睡眠.打开这个开关即可防止自动睡眠. Link:https://www.cnb ...
- IIncrementalGenerator 解析 ValueTuple 的定义
本文将告诉大家如何在分析器里面解析代码里面对于 ValueTuple 的定义,包括如何获取 ValueTuple 里面的 Item 的类型和命名 开始之前先创建一个用来被分析的项目,在这个项目里面定义 ...
- WPF 从 RGB 字符串转纯色颜色画刷的方法
本文告诉大家几个方法用来从 RGB 字符串转纯色的 SolidColorBrush 画刷 在 Windows 下,约定的编程规范里,颜色的 RGB 的字符串表示方法是 #[A]RGB 的格式,一定是 ...
- Python使用HTMLTestRunner运行所有用例并产生报告
#coding:utf-8import unittestimport osimport sysimport HTMLTestRunnercase_path = os.path.join(os.path ...