题目:

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. 力扣48(java)-旋转图像(中等)

    题目: 给定一个 n × n 的二维矩阵 matrix 表示一个图像.请你将图像顺时针旋转 90 度. 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要 使用另一个矩阵来旋转图像 ...

  2. DataFunTalk:阿里建设一站式实时数仓的经验分享

    简介: 本文内容整理于阿里资深技术专家姜伟华在DataFunTalk上的演讲,为大家介绍阿里巴巴基于一站式实时数仓Hologres建设实时数仓的经验和解决方案. 导读:大数据计算正从规模化走向实时化, ...

  3. 终于要跟大家见面了,Flink 面试指南

    面试,一个令人大多数同学头疼的问题,要么成功进入心仪公司,要么沮丧与其失之交臂.但是,如果能在面试前就能知道面试官将会问的问题,然后可以好好提前准备,这种感觉是不是特别棒? 之前社区帮大家汇总了目前 ...

  4. 【产品能力深度解读】连续入围Gartner魔力象限的Quick BI有何魔力?

    简介: 国际权威分析机构Gartner发布2021年商业智能和分析平台魔力象限报告,阿里云Quick BI再度入选,并继续成为该领域魔力象限唯一入选的中国企业. Quick BI凭借在增强分析能力上的 ...

  5. dotnet 在 UOS 国产系统上使用 MonoDevelop 进行拖控件开发 GTK 应用

    先从一个 Hello World 应用开始,试试和古老的 WinForms 一样的拖控件式开发 在创建完成一个 GTK# 2.0 应用之后,咱可以试试开始拖控件的开发,当然这个开发方式开发出来的应用界 ...

  6. 如何参与 .NET 的开发和设计

    现在 dotnet 属于 dotnet 基金会,所有开发者都可以向 dotnet 贡献代码和参与 .NET 的设计,参与路线决策.本文来告诉大家一些基本玩法,带着小伙伴们入坑 注意哦,参与 dotne ...

  7. 一个 5.2k+ Star 的微服务商城系统

    大家好,我是 Java陈序员. 自从微服务开始流行之后,各种商城系统.外卖系统可是层出不穷.面试的时候人人都说做过商城系统,导致现在一听电商系统就是烂大街的项目. 黑格尔曾经说过:存在即合理.电商系统 ...

  8. 创建第一个springmvc程序

    创建第一个springmvc程序 1.创建父项目文件,导入依赖,删除src文件夹 pom.xml文件 <dependencies> <dependency> <group ...

  9. 【Python爬虫案例】用python爬1000条哔哩哔哩搜索结果

    目录 一.爬取目标 二.讲解代码 三.同步讲解视频 四.完整源码 一.爬取目标 大家好,我是 @马哥python说 ,一名10年程序猿. 今天分享一期爬虫的案例,用python爬哔哩哔哩的搜索结果,也 ...

  10. java代码审计-某酒店管理系统

    java代码审计-某酒店后台管理系统 目录 java代码审计-某酒店后台管理系统 1.简介 2.文件上传漏洞 3.CSRF漏洞 4.存储型XSS 1.简介 文章只作研究学习,请勿非法渗透测试: 该系统 ...