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.
  • 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]
]

题目标签:Array

  这道题目和前面那一题本质一摸一样,不同之处是,这题每一个数字只能用一次,之前那题可以无限用;这一题给的array里有重复的,之前那题没有。所以只要之前那题做了,稍微改动一下就可以了。把递归下去的数字index 从i 改成 i + 1,因为这里不需要重复的数字了。再有就是要设一个条件,如果一个重复的数字,不是出现在第一位的话,就跳过它。

Java Solution:

Runtime beats 83.86%

完成日期:07/17/2017

关键词:Array

关键点:Backtracking with sorted array

 public class Solution
{
public List<List<Integer>> combinationSum2(int[] candidates, int target)
{
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(candidates);
backtrack(list, new ArrayList<>(), candidates, target, 0); return list;
} public boolean backtrack(List<List<Integer>> list, List<Integer> tempList,
int[] nums, int remain, int start)
{
if(remain < 0) // if remain is 0 or less than 0, meaning the rest numbers are even greater
return false; // therefore, no need to continue the loop, return false
else if(remain == 0)
{
list.add(new ArrayList<>(tempList));
return false;
}
else
{
for(int i=start; i<nums.length; i++)
{
if(i > start && nums[i] == nums[i-1])
continue;
boolean flag;
tempList.add(nums[i]);
flag = backtrack(list, tempList, nums, remain - nums[i], i+1); // i + 1 because we cannot use same number.
tempList.remove(tempList.size() - 1); if(!flag) // if find a sum or fail to find a sum, there is no need to continue
break;// because it is a sorted array with no duplicates, the rest numbers are even greater.
} return true; // return true because previous tempList didn't find a sum or fail a sum
} }
}

参考资料:

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

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 40. Combination Sum II (组合的和之二)的更多相关文章

  1. [leetcode]40. Combination Sum II组合之和之二

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

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

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

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

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

  4. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  5. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

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

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

  7. Leetcode 40. Combination Sum II

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

  8. leetcode 40 Combination Sum II --- java

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

  9. [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...

随机推荐

  1. 史上最全CentOS安装教程,图文结合

    这是我最近整理的一份最全的CentOS安装步骤,亲自测试步骤,步步都有截图,步骤清晰.按此教程可轻松装机,并且安装成功的主机能访问外部网络. 闲话不说,首先介绍一下本教程用到工具: VMware Wo ...

  2. angular 时间戳转换

    .filter('getWeek', function() { return function(input) { var date = new Date(input * 1000); var week ...

  3. ToStringBuilder学习总结

    一.简介与引入   1.ToStringBuilder.HashCodeBuilder.EqualsBuilder.ToStringStyle.ReflectionToStringBuilder.Co ...

  4. 原型那些事 - JavaScript深入浅出(三)

    前两次总结了JavaScript中的基本数据类型(值类型<引用类型>,引用类型<复杂值>)以及他们在内存中的存储,对内存空间有了一个简单的了解,以及第二次总结了this深入浅出 ...

  5. IDEA用maven创建springMVC项目和配置

    工具准备:IDEA2016.3 Java jdk 1.8 1.DEA创建项目 新建一个maven project,并且选择webapp原型.  然后点击next  这里的GroupId和Artifac ...

  6. asp.net mvc项目实记-开启伪静态-Bundle压缩css,js

    百度这些东西,还是会浪费了一些不必要的时间,记录记录以备后续 一.开启伪静态 如果不在web.config中配置管道开关则伪静态无效 首先在RouteConfig.cs中中注册路由 routes.Ma ...

  7. E - 钱币兑换问题

          在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input每行只有一个正整数N,N小于32768. Output对应每个输入,输出兑换方 ...

  8. css3动画:弹出式菜单

    css3动画:弹出式菜单 今天主要来讲讲transition和transform结合做的动画,会举一些现在(2017年)常见的动画例子. 注:本人也接触css3不久,如果写的有纰漏请指出,不喜勿喷. ...

  9. 配置 VirtualBox backend - 每天5分钟玩转 Docker 容器技术(75)

    Rexy-Ray 支持多种 backend,上一节我们已经安装配置了 Rex-Ray,今天演示如何配置 VirtualBox backend. 在 VirtualBox 宿主机,即我的笔记本上启动 v ...

  10. 每周分享之 二 http协议(1)

    本次分享http协议,共分为三部分,这是第一部分,主要讲解http的发展历程,各个版本,以及各个版本的特点. 一:http/0.9 最早版本是1991年发布的0.9版.该版本极其简单,只有一个命令GE ...