题目:

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

Each number in candidates may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

Example 2:

Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
  [1,2,2],
  [5]
]

分析:

本题是LeetCode 39. Combination Sum 组合总和 (C++/Java)的扩展,我们还是利用39题中的方法来做这道题。

只不过本题所给的数字有重复,且要求最后的结果中不能有重复的,例如例子1,有两个1,他们都可以和7组合成8,如果按照39题搜索的方法就会产生重复结果。

其中一个办法就是将搜索生成的结果加入到set中,这样集合类会自动帮我们去重。

此外我们还可以在每一轮搜索时,当发现有相同元素时,直接跳过本次搜素,这样就不会产生重复的结果了。

程序:

C++

class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> curr;
sort(candidates.begin(), candidates.end());
dfs(candidates, target, 0, res, curr);
return res;
}
void dfs(vector<int>& candidates, int target, int index, vector<vector<int>>& res, vector<int> curr){
if(target == 0){
res.push_back(curr);
return;
}
for(int i = index; i < candidates.size(); ++i){
if(candidates[i] > target)
return;
if(i > index && candidates[i] == candidates[i-1])
continue;
curr.push_back(candidates[i]);
dfs(candidates, target-candidates[i], i+1, res, curr);
curr.pop_back();
}
}
};

Java

class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> curr = new LinkedList<>();
Arrays.sort(candidates);
dfs(candidates, target, 0, res, curr);
return res;
}
private void dfs(int[] candidates, int target, int index, List<List<Integer>> res, LinkedList<Integer> curr){
if(target == 0){
res.add(new LinkedList<>(curr));
return;
}
for(int i = index; i < candidates.length; ++i){
if(candidates[i] > target)
return;
if(i > index && candidates[i] == candidates[i-1])
continue;
curr.addLast(candidates[i]);
dfs(candidates, target-candidates[i], i+1, res, curr);
curr.removeLast();
}
}
}

LeetCode 40. Combination Sum II 组合总和 II (C++/Java)的更多相关文章

  1. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  2. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  4. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  5. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  7. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  8. LeetCode 40. Combination Sum II (组合的和之二)

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

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

    这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...

  10. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

随机推荐

  1. 力扣578(MySQL)-查询回答率最高的问题(中等)

    题目: 从 survey_log 表中获得回答率最高的问题,survey_log 表包含这些列:id, action, question_id, answer_id, q_num, timestamp ...

  2. 直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)

    简介: 近期,来自 Koordinator 社区的两位技术专家从项目的架构和特性出发,分享了 Koordinator 是如何应对混部场景下的挑战,特别是提升混部场景下工作负载的运行的效率和稳定性,以及 ...

  3. 基于Delta lake、Hudi格式的湖仓一体方案

    ​简介: Delta Lake 和 Hudi 是流行的开放格式的存储层,为数据湖同时提供流式和批处理的操作,这允许我们在数据湖上直接运行 BI 等应用,让数据分析师可以即时查询新的实时数据,从而对您的 ...

  4. [TP5] 浅谈 ThinkPHP 的 Hook 行为事件及监听执行

    TP5 中使用 \think\Hook::add('xx', '\app\xxx\behavior\Xx') 注册行为. 也可以在 application/tags.php 中统一注册. 在需要监听执 ...

  5. WPF 笔迹触摸点收集工具

    本文来安利大家一个工具,可以用来收集笔迹的触摸点,这个工具完全开源 在开始之前先看一下工具的界面 实现方式其实就在触摸的时候收集触摸点信息,上面的工具有很多功能都没有实现的.笔迹绘制的功能使用 WPF ...

  6. 从改一个老项目开始的PHP踩坑记

    php所有版本的地址: https://windows.php.net/downloads/releases/archives/ 访问控制器时省略了index.php报No input file sp ...

  7. 大模型_2.1:Prompt进阶

    目录: 1.Prompt frameWork 2.Prompt 结构化格式 3.如何写好结构化 Prompt ? 4.Zero-Shot Prompts 5.Few-Shot Prompting 6. ...

  8. 设置WordPress文章关键词自动获取,文章所属分类名称,描述自动获取文章内容,给文章的图片自动加上AlT标签

    最近在优化网站,SEO优化标准:每一篇文章都要有关键词,关键词的个数为3到6个.每一篇文章都要有描述,描述的字数为汉字在70~80之间,在160个字符之间.每一篇文章的图片都要有Alt标签,自动给图片 ...

  9. java的jdbc插入的时候,遇到null情况报错问题

    分析原因: 在执行SQL时MyBatis会自动通过对象中的属性给SQL中参数赋值,它会自动将Java类型转换成数据库的类型.而一旦传入的是null它就无法准确判断这个类型应该是什么,就有可能将类型转换 ...

  10. installshield 安装jdk并配置环境变量

    今天来通过installshield安装jdk以及配置环境变量,本质上是调用第三方安装程序. 首先将jdk的安装文件添加到我们的安装程序中 然后编写我们的脚本 选择BEHAVIOR AND LOGIC ...