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. @Resource或者@Autowired作用/Spring中@Autowired注解、@Resource注解的区别

    @Resource或者@Autowired作用不用写set get就能注入,当然,前提是你已经开启了注解功能. spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定 ...

  2. 微信小程序挑一挑辅助

    1.窗体 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;usi ...

  3. java 虚函数

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

  4. STM32关总中断的语句

    __set_PRIMASK();//关总中断 /************ **********/ __set_PRIMASK();//开总中断

  5. js对象成员的访问

    var obj={0:1,1:'id'};//访问对象 obj['0'];//用[]来访问,不要用.的方式访问 -----对象与字符串的转换----- node.js JS对象和JSON字符串之间的转 ...

  6. 【UNIX网络编程(四)】TCP套接字编程具体分析

    引言: 套接字编程事实上跟进程间通信有一定的相似性,可能也正由于此.stevens这位大神才会将套接字编程与进程间的通信都归为"网络编程",并分别写成了两本书<UNP1> ...

  7. Atitit.vod 视频播放系统 影吧系统的架构图 架构体系 解决方案

    Atitit.vod 视频播放系统 影吧系统的架构图 架构体系 解决方案 1. 运行平台:跨平台 android ios pc mobile 1.1. -------------前端 界面------ ...

  8. linux 查看可执行文件动态链接库相关信息(转)

    转自 http://blog.sina.com.cn/s/blog_67eb1f2f0100mgd8.html ldd <可执行文件名>       查看可执行文件链接了哪些  系统动态链 ...

  9. binutils工具集之---objcopy,ranlib,size,strings,strip

    objcopy的作用是拷贝一个目标文件的内容到另一个目标文件中.objcopy使用GNU BFD库去读或写目标文件.objcopy可以使用不同于源目标文件的格式来写目的目标文件(也即是说可以将一种格式 ...

  10. Centos6.5 --配置 vsftp server

    事实上我这么懒得人是不想配置什么ftpserver的(毕竟动起来都认为麻烦).可是因为本菜鸟才刚開始步入linux的大坑.尽管有装Centos7的真机,可是因为一时还是脱离不了Windows平台,所以 ...