LeetCode OJ 40. Combination Sum II

时间:2022-12-12 12:38:37

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. &lbrack;Leetcode&rsqb;&lbrack;Python&rsqb;40&colon; Combination Sum II

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

  2. 【一天一道LeetCode】&num;40&period; Combination Sum II

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

  3. 【LeetCode】40&period; Combination Sum II &lpar;2 solutions&rpar;

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

  4. LeetCode:40&period; Combination Sum II(Medium)

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

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

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

  6. LeetCode OJ:Combination Sum II &lpar;组合之和 II&rpar;

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

  7. &lbrack;array&rsqb; leetcode - 40&period; Combination Sum II - Medium

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

  8. leetcode 39&period; Combination Sum 、40&period; Combination Sum II 、216&period; Combination Sum III

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

  9. &lbrack;LeetCode&rsqb; 40&period; Combination Sum II 组合之和之二

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

随机推荐

  1. 解决Ubuntu Server 12&period;04 在Hyper-v 2012 R2中不能使用动态内存的问题

    前言 全新Hyper-v 2012 R2终于开始支持在Linux的VPS中使用动态内存,可以大大优化服务器的资源分配,小弟我兴奋不已,于是抽空时间赶紧升级到 2012 R2,好好整理一番内存分配,不过 ...

  2. Oracle之存储过程

    1.存储过程创建 oracle中创建存储过程的语法如下: CREATE [OR REPLACE] PROCEDURE PRO_NAME[(parameter1[,parameter2]...)]is| ...

  3. Oracle 体系结构及安全管理

    1 oracle数据库服务器构成:数据库和实例2 oracle内部结构: 物理存储结构: 数据文件(xxx.dbf):存放数据 控制文件(xxx.ctl):控制数据库的完整性恢复数据或使用的日志文件 ...

  4. 如何合并相同数据并转置&lpar;mysql&rpar;实现

    上次参加天猫大数据竞赛 是预测用户会买哪些牌子给用户推荐 拥有的字段 user_id,brand_id 最后要转成如下格式,比如用户1,要给他推荐2,3,4号品牌 原来的数据是 user_id,bra ...

  5. Java 反射 设计模式 动态代理机制详解 &lbrack; 转载 &rsqb;

    Java 反射 设计模式 动态代理机制详解 [ 转载 ] @author 亦山 原文链接:http://blog.csdn.net/luanlouis/article/details/24589193 ...

  6. 从deque到std&colon;&colon;stack,std&colon;&colon;queue,再到iOS 中NSArray(CFArray)

    从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...

  7. 推荐——基于python

    资料来源: <集体智慧编程>&网络 一.推荐系统 概述 定义 *定义: 推荐系统属于资讯过滤的一种应用. 推荐系统能够将可能受喜好的资讯或实物(例如:电影.电视节目.音乐.书 ...

  8. C&num; 解析torrent文件

    基础知识: torrent文件信息存储格式: bencoding是一种以简洁格式指定和组织数据的方法.支持下列类型:字节串.整数.列表和字典. 1 字符串存储格式:  <字符串的长度>:& ...

  9. setdest 和cbrgen工具的使用,出现的错误

    在路径 ~/ns-2.34/indep-utils/cmu-scen-gen/setdest下运行 ./setdest -n 250 -p 0.0 -M 10.0 -t 10 -x 1500 -y 1 ...

  10. JAVA中使用LOG4J记录日志

    在项目开发中,记录错误日志是一个很有必要功能.一是方便调试:二是便于发现系统运行过程中的错误:三是存储业务数据,便于后期分析: 在java中,记录日志,有很多种方式. 比如,自己实现. 自己写类,将日 ...