题目:

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. Django框架——ORM执行SQL语句、神奇的双下划线、外键字段的创建、跨表查询、进阶操作

    ORM执行SQL语句 有时候ORM的操作效率可能偏低 我们是可以自己编写SQL的 方式一: models.User.objects.raw('select * from app01_user') 方式 ...

  2. Thinkphp5.x全漏洞复现分析

    基础知识 命名空间和子命名空间 我们可以把namespace理解为一个单独的空间,事实上它也就是一个空间而已,子命名空间那就是空间里再划分几个小空间,举个例子: <?php namespace ...

  3. 【笔记】go语言--Map

    go语言--Map //基本结构,定义 m := map[string] string { "name" : "ccmouse",//这些是无序的,是hashm ...

  4. 力扣686(java)-重复叠加字符串匹配(中等)

    题目: 给定两个字符串 a 和 b,寻找重复叠加字符串 a 的最小次数,使得字符串 b 成为叠加后的字符串 a 的子串,如果不存在则返回 -1. 注意:字符串 "abc" 重复叠加 ...

  5. Koordinator 0.6:企业级容器调度系统解决方案,引入 CPU 精细编排、资源预留与全新的重调度框架

    简介: 经过社区多位成员的贡献,Koordinator 0.6 版本正式发布.相较于上一个版本 0.5,新版本进一步完善了 CPU 精细化编排能力,更好的兼容原生用法:支持了资源预留的能力(Reser ...

  6. 模型代码联动难? BizWorks来助力

    简介: 本文介绍了业务模型设计和实现保持一致的重要性以及实际落地可能遇到的问题,以及BizWorks如何设计并提供一种双向联动能力, 通过BizWorks Toolkit(IDE 插件) 来解决和优化 ...

  7. 深度解读 MongoDB 最全面的增强版本 4.4 新特性

    MongoDB 在今年正式发布了新的 4.4 大版本,这次的发布包含众多的增强 Feature,可以称之为是一个维护性的版本,而且是一个用户期待已久的维护性版本,MongoDB 官方也把这次发布称为「 ...

  8. 让微服务开源更普惠,阿里云微服务引擎MSE全球开服

    ​简介:MSE 于2020年10月在国内开启商业化服务,目前已吸引近万客户使用,用于在云上更低成本构建.更稳定运行微服务架构.此次,MSE 向阿里云国际站开放服务,旨在帮助更多客户享受到更加普惠的微服 ...

  9. 基于WASM的无侵入式全链路A/B Test实践

    简介: 我们都知道,服务网格(ServiceMesh)可以为运行其上的微服务提供无侵入式的流量治理能力.通过配置VirtualService和DestinationRule,即可实现流量管理.超时重试 ...

  10. [GPT] nodejs 有哪些类似 jquery 语法的 html 解析库

      在Node.js中,有一些类似jQuery语法的HTML解析库可供选择. 以下是其中几个常用的库: 1. Cheerio: Cheerio是一个快速.灵活且易于使用的HTML解析库,它提供了类似于 ...