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代码】

  1. public class Solution {
  2. public List<List<Integer>> combinationSum2(int[] candidates, int target) {
  3. Arrays.sort(candidates);
  4. return combination(candidates, target, 0);
  5. }
  6.  
  7. public List<List<Integer>> combination(int[] candidates, int target, int start) {
  8. List<List<Integer>> list = new ArrayList<>();
  9. if(candidates == null || candidates.length == 0) return list;
  10. int last = 0;
  11.  
  12. for(int i = start; i < candidates.length; i++){
  13. if(candidates[i] == last) continue;
  14. if(candidates[i] < target){
  15. List<List<Integer>> tlist = combination(candidates, target - candidates[i], i+1);
  16. if(tlist.size() > 0){
  17. for(List<Integer> alist : tlist){
  18. alist.add(0, candidates[i]);
  19. }
  20. list.addAll(tlist);
  21. }
  22. }
  23. else if(candidates[i] == target){
  24. List<Integer> tlist = new LinkedList<>();
  25. tlist.add(target);
  26. list.add(tlist);
  27. }
  28. else break;
  29. last = candidates[i];
  30. }
  31. return list;
  32. }
  33. }

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. Genymotion下载失败解决方法

    Genymotion下载虚拟机版本时会很慢,而且经常下载失败 解决方法如下: 1.先去选择下载你需要的版本,之后会下载(很慢),或者失败. 2.到C:\Users\yourname\AppData\L ...

  2. ueditor的工具按钮配置

    定制工具栏图标 UEditor 工具栏上的按钮列表可以自定义配置,只需要通过修改配置项就可以实现需求 配置项修改说明 修改配置项的方法: 1. 方法一:修改 ueditor.config.js 里面的 ...

  3. 【IE6的疯狂之六】li在IE中底部3像素的BUG(增加浮动解决问题)

    今天开发项目中碰到一个li在IE中的BUG,先来看设计原型(如图:) 两个红色中间是<li>1px的底边框: 我写的代码如下: ============================== ...

  4. 【android错误】bitmap size exceeds 32bits

    使用图片缩放时遇到这么个问题: java.lang.IllegalArgumentException: bitmap size exceeds 32bits 后来一行行查代码,发现原来是 scale ...

  5. 博客搬到CSDN了,以后就老实的呆在这儿吧~~

    几年前读书的时候就自己在做独立的个人博客网站,重做 + 改版好多次,域名也换了好几个- 163fly.com.godbz.com.zhouz.me ... 都是我曾经用过的域名,都放弃了- 发现到头来 ...

  6. python中判断语句用两个or连接的奇葩

    学python的时候犯的一个错误,放在这吧.就是在循环某个列表的时候不要去操作它,这是容易忽略的一个地方.所以如果要操作某个列表本身,那么先把该列表copy一份,然后再读取的时候读copy的那份.操作 ...

  7. Java之JSP基础语法

    1.JSP页面元素简介及page指令     2.JSP注释,3种不同注释 <!--  我是HTML注释,在客户端可见 --> <%--我是JSP注释,在客户端不可见 --%> ...

  8. scala实现快速排序

    scala> def qSort(a: List[Int]): List[Int] = { | ) a | else qSort( a.filter(a.head > _ )) ++ | ...

  9. xml文件查找重复元素(超简单版)

    使用WPS,新建一个表格文件,将xml拖入表格,点数据,选中存在重复项的列,点高亮重复项,OK.

  10. sqlite增删改查

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...