题目:

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)的更多相关文章

  1. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  2. 【LeetCode】Combination Sum(组合总和)

    这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...

  3. [leetcode]39. Combination Sum组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  4. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  5. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  6. LeetCode 39. Combination Sum (组合的和)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  7. LeetCode 39 Combination Sum(满足求和等于target的所有组合)

    题目链接: https://leetcode.com/problems/combination-sum/?tab=Description   Problem: 给定数组并且给定一个target,求出所 ...

  8. leetcode 39 Combination Sum --- java

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. Java [Leetcode 39]Combination Sum

    题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in  ...

  10. 039 Combination Sum 组合总和

    给一组候选数字(C)(没有重复)并给一个目标数字(T),找出 C 中所有唯一的组合使得它们的和为 T.可以从 C 无限次数中选择相同的数字.说明:    所有数字(包括目标)都是正整数.    解集合 ...

随机推荐

  1. D365增加Model reference,解决does not designate a class or table编译错误问题

    当我们导入基础数据时,需要创建一些基本的Emplyee信息,当引用到HcmHireNewWorkerContract和HcmWorkerTransition时,提示如下错误: 'HcmHireNewW ...

  2. 飞桨PaddlePaddle的安装

    飞桨PaddlePaddle的安装 MacOS 下的 PIP 安装 一.环境准备 1.1 如何查看您的环境 可以使用以下命令查看本机的操作系统和位数信息: uname -m && ca ...

  3. 优化搜索排序结果从而“ 提升CTR、CVR业务指标”

    简介: 搭建搜索功能不难,难的是如何提高搜索质量,帮助用户快速找到心中所想的内容或商品,那么搜索结果的相关性排序则是影响用户体验最关键的一环,本文通过阿里云开放搜索电商行业解决方案和大家聊一聊如何优化 ...

  4. 当Java遇上机密计算,又一段奇幻之旅开始了!

    ​简介: 汪少军:如何为Java业务提供机密计算保护? ​ 写在前面 在信息世界里,数据存在三种状态: 存储态.传输态和计算态.存储在数据库或磁盘中的数据属于存储状态,在网络中传输的数据属于传输状态, ...

  5. 技术干货 | Native 页面下如何实现导航栏的定制化开发?

    ​简介: 通过不同实际场景的描述,供大家参考完成 Native 页面的定制化开发. ​ 很多 mPaaS Coder 在接入 H5 容器后都会对容器的导航栏进行深度定制,本文旨在通过不同实际场景的描述 ...

  6. IphoneX(10) 重启/关机, 强制重启/关机

    正常关机是同时长按 音量+ 和 右侧电源键,屏幕出现滑动按钮进行关机. 注意截图是同时短按 音量+ 和 右侧电源键. 强制关机是按照顺序按三个键:音量+   音量-  长按右侧键 Other:苹果X怎 ...

  7. dotnet OpenXML 读取 PPT 内嵌 xlsx 格式 Excel 表格的信息

    在 Office 中,可以在 PPT 里面插入表格,插入表格有好多不同的方法,对应 OpenXML 文档存储的更多不同的方式.本文来介绍如何读取 PPT 内嵌 xlsx 格式的表格的方法 读取方法和 ...

  8. 【内存优化】Oracle 的SGA与Linux的shmall和shmmax的关联

    查看linux下的Oracle共享内存段 [oracle@oradb ~]$ ipcs -m ------ Shared Memory Segments -------- key shmid owne ...

  9. 应急响应--windows入侵排查

  10. 08. C语言函数

    [函数基础] 函数用于将程序代码分类管理,实现不同功能的代码放在不同函数内,一个函数等于一种功能,其它函数可以调用本函数执行. C语言规定所有的指令数据必须定义在函数内部,比如之前介绍的程序执行流程控 ...