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. kettle--组件(3)--行转列

    组件图如下: 以上操作可以这么理解: IF(DATA1=DATA4) THEN DATA2=DATA3 也就是关键字值的数值会与关键字段的数值匹配,匹配上了就显示数据value filedname所填 ...

  2. Python selenium -- cookie处理

    转自:http://www.cnblogs.com/fnng/p/3269450.html 本节重点: driver.get_cookies() 获得cookie信息 add_cookie(cooki ...

  3. java 虚函数

    猜猜这里的代码输出的结果是多少? package test; public class ConstructorExample { static class Foo { int i; Foo() { i ...

  4. freeswitch 音 视频 支持的编码

    FreeSWITCH 支持很多的语音编解码:[13] PCMU – G.711 µ-law PCMA – G.711 A-law G.722 G.722.1 G.722.1c G.726 G.726  ...

  5. gdi软光栅化注意事项

    1,opengl viewport原点在左下角,而gdi画图api原点在左上角,所以在实现了整个opengl管线,最后将点通过gdi函数画到屏幕时要进行临时转化. 2,注意gdi画点的api传入的颜色 ...

  6. Sublime Text的列模式

    Sublime Text的列模式如何操作? 听语音 | 浏览:6551 | 更新:2014-12-09 13:27 | 标签:软件 Sublime Text的列模式如何操作?各个系统不一样,请跟进系统 ...

  7. C++忽略字符大小写比较

    在项目中用到对两个字符串进行忽略大小写的比较,有两个方法实现 1.使用C++提供的忽略大小写比较函数实现 代码实现: /* 功能 :忽略大小写进行字符串比较 */ #ifdef __LINUX__ # ...

  8. Powershell对象条件查询筛选

    在 Windows PowerShell 中,与所需的对象数量相比,通常生成的对象数量以及要传递给管道的对象数量要多得多.可以使用 Format cmdlet 来指定要显示的特定对象的属性,但这并不能 ...

  9. java对象实现深复制的方法

    p2 = (Person)org.apache.commons.lang3.ObjectUtils.cloneBean(p); Person p2 = new Person(); p2 = (Pers ...

  10. UITextField小结

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...