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, a1a2 ≤ … ≤ 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]

Have you met this question in a real interview?
 
Analysis:
Since there are duplicates, for each value, we get the number of elements that are this value. We sort the array, at each step in the recursion, we try to use 0 to elementNumOfCurValue number of this value. For next step, we directly skip to the next value.
 
Solution:
 public class Solution {
public List<List<Integer>> combinationSum2(int[] num, int target) {
int[] candidates = num;
List<List<Integer>> resSet = new ArrayList<List<Integer>>();
List<Integer> curRes = new ArrayList<Integer>();
if (candidates.length==0) return resSet;
Arrays.sort(candidates);
int cur=0,end=candidates.length-1;
for (int i=0;i<candidates.length;i++)
if (candidates[i]>target){
end = i-1;
break;
} sumRecur(candidates,cur,end,target,resSet,curRes); return resSet; } public void sumRecur(int[] candidates, int cur, int end, int valLeft, List<List<Integer>> resSet, List<Integer> curRes){
if (valLeft==0){
List<Integer> temp = new ArrayList<Integer>();
temp.addAll(curRes);
resSet.add(temp);
return;
} if (cur>end) return; int newLeft = valLeft;
int curLen = curRes.size();
int nextIndex = cur;
while (nextIndex<=end && candidates[nextIndex]==candidates[cur]) nextIndex++; for (int i=cur;i<nextIndex;i++)
if (newLeft>=candidates[i]){
curRes.add(candidates[i]);
newLeft -= candidates[i];
sumRecur(candidates,nextIndex,end,newLeft,resSet,curRes);
} else
break; while (curRes.size()!=curLen) curRes.remove(curRes.size()-1); sumRecur(candidates,nextIndex,end,valLeft,resSet,curRes);
}
}

Leetcode-Combinations Sum II的更多相关文章

  1. LeetCode: Combination Sum II 解题报告

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

  2. [leetcode]Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  3. LeetCode: Path Sum II 解题报告

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  4. [LeetCode] Combination Sum II 组合之和之二

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

  5. [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  6. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  7. Leetcode Combination Sum II

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

  8. LeetCode Two Sum II - Input array is sorted

    原题链接在这里:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 题目: Given an array of intege ...

  9. [LeetCode] Combination Sum II (递归)

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

  10. [leetcode]Path Sum II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

随机推荐

  1. jquery.flexslider-min.js实现banner轮播图效果

    实现方法 引用jQuery和flexslider.js到你的页面 <script type="text/javascript" src="js/jquery-1.7 ...

  2. SQL面试题: 数据库中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列 ,当B列大于C列时选择B列否则选择C列 ,

    1.用一条sql语句 select (case when a>b then a else b end ),(case when b>c then b esle c end)  from 表 ...

  3. 解决Win10系统下 C# DateTime 出现星期几的问题 解决ASP.NET MVC 接受Request Playload参数问题

    解决Win10系统下 C# DateTime 出现星期几的问题 昨天晚上写代码的时候偶然发现 DateTime 里出现了星期几,当时一阵凌乱,去网上百度没有详细解决办法,很多人说可以用用 ToStri ...

  4. java中finalkeyword使用说明

    必须在域的定义处或者每一个构造器中用表达式对final进行赋值,这正是final域在使用前总是被初始化的原因所在.

  5. Navicat for MySQL再谈之无奈之下还是去安装Navicat Premium

    不多说,直接上干货! 首先,Navicat for MySQL没有查看数据库属性. 其次,没有这个功能多和强大,在走过一段弯路之后,果断放弃Navicat for MySQL,而使用Navicat P ...

  6. tomcat 工作原理简析

    https://github.com/HappyTomas/another-tutorial-about-java-web/blob/master/00-08.md 在00-02.理解HTTP中给出了 ...

  7. 反射学习2-通过反射机制动态获取属性的值模拟Struts的自动赋值

    一.准备知识:   Java反射机制   处理事务的JavaBean   String的操作常用方法 二.模拟步骤   这里我们通过反射机制动态获取属性的值模拟Struts中的自动赋值. 1.首先创建 ...

  8. redis命令_ZADD

    ZADD key score member [[score member] [score member] ...] 将一个或多个 member 元素及其 score 值加入到有序集 key 当中. 如 ...

  9. PHP——smarty模板(第一天)

    smarty.class.php 主要的东西放在类里面templates 放模板templates_c 放缓存 类里面 $smarty->assign("author",&q ...

  10. Storm学习笔记——简介

    1. 简介 流式计算的历史 早在7.8年前诸如UC伯克利.斯坦福等大学就开始了对流式数据处理的研究,但是由于更多的关注于金融行业的业务场景或者互联网流量监控的业务场景,以及当时互联网数据场景的限制,造 ...