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

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

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 10,1,2,7,6,1,5 and target 8
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6]

Subscribe to see which companies asked this question

【题目分析】

相比较上一个题:39. Combination Sum,这个题目在一些细节上发生了变化,即最后生成的组合中某个数字出现的最大重复次数是给的候选数组中该数字的重复次数。

【思路】

我们在上个题的基础上进行改动,上个算法是采用了递归的方式。首先把数组排序,然后遍历数组中的每一个数字n,如果该数字小于当前的目标值,就在当前数字开始向后的那部分数中生成目标值等于target-n的组合,再把当前值加入到所有组合中,这个过程会保证每个数字可以重复任意需要的次数。如果每次递归时,改变递归开始的数字的下标,使其不包含当前数字,那么该数字就不会在最后的生成的组合中重复多次。但是这样存在一个问题就是,生成的某些组合会出现多次。

例如这样一个序列:[10,1,2,7,6,1,5] 排序后为:[1,1,2,5,6,7,10]. 如果只改变每次递归时候选数字开始的数组下标值,结果是这样的:

[1,1,6] [1,2,5] [1,7] [1,2,5] [1,7] [2,6]

我们发现[1,2,5] [1,7]重复出现了,这是为什么呢?因为在候选数组中1出现了两次,那么在见到第一个1时我们已经生成了关于1的所有组合,当遍历到第二个1时,自然会导致部分组合出现重复。解决的办法就是我们记录上一个遍历值的大小,如果当前值和上一个值相同,那么我们就跳过当前值。这样就不会出现生成的组合重复出现的现象。

【java代码】

 public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
return combination(candidates, target, 0);
} public List<List<Integer>> combination(int[] candidates, int target, int start) {
List<List<Integer>> list = new ArrayList<>();
if(candidates == null || candidates.length == 0) return list;
int last = 0; for(int i = start; i < candidates.length; i++){
if(candidates[i] == last) continue;
if(candidates[i] < target){
List<List<Integer>> tlist = combination(candidates, target - candidates[i], i+1);
if(tlist.size() > 0){
for(List<Integer> alist : tlist){
alist.add(0, candidates[i]);
}
list.addAll(tlist);
}
}
else if(candidates[i] == target){
List<Integer> tlist = new LinkedList<>();
tlist.add(target);
list.add(tlist);
}
else break;
last = candidates[i];
}
return list;
}
}

LeetCode OJ 40. Combination Sum II的更多相关文章

  1. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  2. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

  3. 【LeetCode】40. Combination Sum II (2 solutions)

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  4. LeetCode:40. Combination Sum II(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...

  5. 【LeetCode】40. Combination Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...

  6. LeetCode OJ:Combination Sum II (组合之和 II)

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

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

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

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

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

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

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

随机推荐

  1. find用法积累

    查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...

  2. 在虚拟机上配置linux lab的相关经验

    最近一直在研究怎样在嵌入式开发板上移植linux嵌入式系统,但是不太想花费太多钱购买开发板.然后在网上搜索相关的arm模拟器.有qemu,skyeye,armulator等,在按照网上教程一步一步实践 ...

  3. CodeForces 669C Little Artem and Matrix GNU

    模拟. 把操作记录一下,倒着复原回去. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cs ...

  4. STM32F2系列系统时钟默认配置

    新到一家公司后,有个项目要用到STM32F207Vx单片机,找到网上的例子照猫画虎的写了几个例子,比如ADC,可是到了ADC多通道转换的时候就有点傻眼了,这里面的时钟跑的到底是多少M呢?单片机外挂的时 ...

  5. 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义。

    说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS0012: 类型“System.Data.Objects.DataClas ...

  6. 为什么Intent传递对象的时候必须要将对象序列化呢?

    Intent可以算是四大组件之间的胶水,比如在Activity1与Activity2之间传递对象的时候,必须要将对象序列化, 可是为什么要将对象序列化呢? Intent在启动其他组件时,会离开当前应用 ...

  7. 安装Mysql,缺少libaio依赖

    安装mysql时报如下错误: xxxxx/mysql/bin/mysqld: error : cannot open shared object file: No such file or direc ...

  8. private set

    表示只读: 例:public string DisplayName { get; private set; }  称为"自动属性" 等同于: private string _Dis ...

  9. 在GNU/Linux下使用命令行自动挂载与卸载USB磁盘

    在命令行环境下如果每次都是靠手动敲入mount与umount命令来挂载与卸载USB磁盘是件很麻烦的事情.尤其是mount命令的参数非常多.比如,磁盘的分区类型(vfat.ntfs等),挂载的目录节点, ...

  10. C# winform Datagridview 标题居中

    DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter