LeetCode OJ 40. Combination Sum II
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的更多相关文章
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 【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 ...
- LeetCode:40. Combination Sum II(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- JS中的call()和apply()方法理解和使用
1.方法定义call方法: 语法:obj.method.call(thisObj[,arg1[, arg2[, [,.argN]]]]) 定义:调用对象(obj)的一个方法(method),以另一个对 ...
- moodle笔记之-数据库操作
<?php require_once('../config.php'); // config.php under root folder require_once($CFG->dirroo ...
- svg图片转换为WEB字体图标
今天我学会了使用字体制作网站 icomoon.io 制作web文本图标.跟我一起学习吧! (1)字体制作网站 icomoon.io 点击 icomoon APP ---> imp ...
- Python 清理HTML标签类似PHP的strip_tags函数功能(二)
没有发现Python 有现成的类似功能模块,所以昨天写了个简单的 strip_tags 但还有些问题,今天应用到采集上时进行了部分功能的完善, 1. 对自闭和标签处理 2. 以及对标签参数的过滤 fr ...
- AngularJS中如何对Controller与Service进行分层设计与编码
初学者的Controller 在我们当接触NG后,如需要通过访问远程的API获取一系列的数据进行显示,通常我的Controller代码会写成下面的样子: angular.module('demo') ...
- C#异常性能影响
何谓异常 很多人在讨论异常的时候很模糊,仿佛所谓异常就是try{}catch{},异常就是Exception,非常的片面,所以导致异常影响性能,XXXX……等很多奇怪的言论,所以在此我意在对异常正名. ...
- Scala 中的 apply 和 update 方法[转]
原文链接:http://blog.csdn.net/lyrebing/article/details/21696581 Scala 是构建在 JVM 上的静态类型的脚本语言,而脚本语言总是会有些约定来 ...
- 浙大 pat 1003 题解
1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- Unity发布安卓无法读取StreamingAssets文件下数据库的问题
在移动端StreamingAssets下的文件是只读的,但大家可能跟我一样遇到了发布安卓以后放在StreamingAssets下的数据库文件一样读取不了, 但其实这个文件夹其实是可以读取到的,所以我们 ...
- delphi倒计时按钮写法
procedure TForm1.FormActivate(Sender: TObject); var i: Integer; begin btn8.Enabled:=False; do begin ...