给定候选号码数组 (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. listen 63

    Danger Explainers Convince Kids Better Than Do Edict Issuers An eight-year-old may view a hammer as ...

  2. LeetCode-5:Longest Palindromic Substring(最长回文子字符串)

    描述:给一个字符串s,查找它的最长的回文子串.s的长度不超过1000. Input: "babad" Output: "bab" Note: "aba ...

  3. poj-1986 Distance Queries(lca+ST+dfs)

    题目链接: Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 11531   Accepted ...

  4. HDU-6035:Colorful Tree(虚树+DP)

    这里有三道长得像的题: 一:HDU6036: There is a tree with nn nodes, each of which has a type of color represented ...

  5. python之系统编程 --进程

    1.调试(PDB) 代码: [root@master gaoji]# vim test2.py 1 #!/usr/local/bin/python3 2 # -*- coding:utf-8 -*- ...

  6. kettle导数到user_用于left join_20160928

    这篇博客主要是给mysql left join做铺垫,需要现在本地数据库创建一个users 数据表 然后去和 test_a03order表  left join 一.首先在local_db数据库先创建 ...

  7. AtCoder Grand Contest 010 C:Cleaning

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_c 题目翻译 给你一棵树,每个点有个权值,每次操作可以选择两个度数为\(1\)的结点,然后让这 ...

  8. Python创建删除文件

    Python代码如下: import os directory = "E:\\学习日志\\" os.chdir(directory) # 改变当前工作目录 cwd = os.get ...

  9. nginx web端口映射

    nginx web端口映射 举例: 问:在一台有外网的宿主机上部署了一个kvm虚拟机,在虚拟机上部署了web,需要外网访问此web,但虚拟机只有内网ip,只和宿主机通,怎么实现外网访问web服务? 答 ...

  10. selenium上传文件,怎么操作

    #通过os.path.abspath()方法,打开图片的绝对路径,然后,定位上传按钮,然后,send_keys()方法中,添加这个文件路径就可以了