给定候选号码数组 (C) 和目标总和数 (T),找出 C 中候选号码总和为 T 的所有唯一组合。
C 中的每个数字只能在组合中使用一次。
注意:
    所有数字(包括目标)都是正整数。
    解决方案集不能包含重复的组合。
例如,给定候选集合 [10, 1, 2, 7, 6, 1, 5] 和目标总和数 8,
可行的集合是:
[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]
详见:https://leetcode.com/problems/combination-sum-ii/description/

Java实现:

class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<Integer> out=new ArrayList<Integer>();
Arrays.sort(candidates);
helper(candidates,target,0,out,res);
return res;
}
private void helper(int[] candidates, int target,int start,List<Integer> out,List<List<Integer>> res){
if(target<0){
return;
}
if(target==0){
res.add(new ArrayList<Integer>(out));
}else{
for(int i=start;i<candidates.length;++i){
if(i>start&&candidates[i]==candidates[i-1]){
continue;
}
out.add(candidates[i]);
helper(candidates,target-candidates[i],i+1,out,res);
out.remove(out.size()-1);
}
}
}
}

参考:http://www.cnblogs.com/grandyang/p/4419386.html

040 Combination Sum II 组合总和 II的更多相关文章

  1. 【LeetCode】Combination Sum II(组合总和 II)

    这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...

  2. 216 Combination Sum III 组合总和 III

    找出所有可能的 k 个数,使其相加之和为 n,只允许使用数字1-9,并且每一种组合中的数字是唯一的.示例 1:输入: k = 3, n = 7输出:[[1,2,4]]示例 2:输入: k = 3, n ...

  3. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  4. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  5. 40. 组合总和 II + 递归 + 回溯 + 记录路径

    40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...

  6. 组合总和 II

    组合总和 II 题目介绍 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...

  7. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  8. LeetCode 040 Combination Sum II

    题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find al ...

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

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

随机推荐

  1. SpringBoot_Exception_01_No plugin found for prefix 'spring-boto' in the current project

    一.异常现象 spingbott项目在eclipse中执行maven命令:spring-boot:run, 出现异常: No plugin found for prefix 'spring-boto' ...

  2. 【Shell】通配符与特殊符号

    ——来自<鸟哥的Linux私房菜> 在 bash 的操作环境中还有一个非常有用的功能,那就是通配符 (wildcard) ! 我们利用 bash 处理数据就更方便了!底下列出一些常用的通配 ...

  3. 使用TortoiseGit同步代码到github远程仓库

    1.clone github上的代码仓库的URL 可以用HTTPS,SSH, or Subversion 2.同步push 到远程仓库时 要用 SSH地址,同生成SSH private key ,在g ...

  4. 四维偏序 CDQ套CDQ

    对CDQ深一步的理解 昨天做了一道CDQ,看了一堆CDQ可做的题,今天又做了一道四维偏序 感觉对CDQ的理解又深了一点,故来写一写现在自己对于CDQ的理解 CDQ其实就是实现了这样的一个问题的转化: ...

  5. python爬虫知识点总结(七)PyQuery详解

    官方学习文档:http://pyquery.readthedocs.io/en/latest/api.html 一.什么是PyQuery? 答:强大有灵活的网页解析库,模仿jQuery实现.如果你觉得 ...

  6. Spring boot 学习八 Springboot的filter

    一:  传统的javaEE增加Filter是在web.xml中配置,如以下代码: <filter> <filter-name>TestFilter</filter-nam ...

  7. Arduino 视频教程

    http://v.youku.com/v_show/id_XNDU1MjI4MzA4.html?from=y1.2-1-176.3.12-2.1-1-1-11

  8. maven 整合shh框架的pom.xml文件配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. CodeForces 1097G. Vladislav and a Great Legend

    题目简述:给定$n \leq 10^5$个节点的树$T = (V, E)$,令$X \subseteq V$表示一个非空节点集合,定义$f(X)$为包含$X$的最小子树的边数.求 $$ \sum_{\ ...

  10. 图解Mysql语句的执行过程

    当我们希望Mysql能够高性能的执行查询语句时,其实最好的方法就是搞清楚Mysql到底是怎样执行查询的.一旦理解这一点,很多的查询优化工作实际上就是遵循一些原则让查询优化器能够按照预想的合理的方式运行 ...