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. Java: AutoCloseable接口

    K7 增加了一些新特性,其中报错AutoCloseable 等.新特性包括如下,下面主要说说AutoCloseable . 在JDK7 中只要实现了AutoCloseable 或Closeable 接 ...

  2. request.getParameter()获取URL中文参数乱码的解决办法

    这个问题耽误好长时间,URL传中文参数出现乱码,就算首次使用request接收就添加 request.setCharacterEncoding("UTf-8"); 依然报错不误. ...

  3. Activiti第二篇【管理流程定义、执行任务和流程实例、流程变量】

    上篇Activiti只是一个快速入门案例,这篇就讲定义.部署.查看任务等等的一些细节[涉及到的数据库表.对象等等]- 管理流程定义 管理流程定义主要涉及到以下的4张表: -- 流程部署相关的表 SEL ...

  4. Oracle总结第二篇【视图、索引、事务、用户权限、批量操作】

    前言 在Oracle总结的第一篇中,我们已经总结了一些常用的SQL相关的知识点了-那么本篇主要总结关于Oralce视图.序列.事务的一些内容- 在数据库中,我们可以把各种的SQL语句分为四大类- (1 ...

  5. JUDE-UML工具软件介绍

    JUDE社区版(不考虑破-解). 现在Jude改名为Astah了.JUDE已停止发展,Astah是它的替代品.Jude有3个版: Professional版, Community版(免费),Share ...

  6. 交互模式下测试python代码及变量的四则运算

    在交互模式下,python代码可以立即执行,所以这很方便我们进行代码测试 1.命令窗口,输入python (如果没配置环境变量则需带python安装目录的绝对路径) >>> 这个就是 ...

  7. Git 基本命令有哪些

    Git 相关命令 git init 初始化一个项目 git clone 利用url 从远程clone下来一个项目 git status 查看当前项目修改状态 git log 查看日志 查看历史记录 g ...

  8. HTML超文本

    1.HTML链接 2.HTML表格 3.HTML图像 4.HTML列表 5.HTML块 6.HTML布局 7.HTML表单 1.HTML链接 (1)给文字及图片添加超链接 < html> ...

  9. Matlab入门学习(文件读写)

    一.save,load >> a=[ ]; >> b=[ ] b = >> save('b.mat','a','b');%a file named b.mat wi ...

  10. 深入理解计算机系统chapter7

    链接:将各种代码和数据部分收集起来并组合成为单一文件的过程,这个文件可被加载到存储器并执行. 在运行时,和一个在存储器中的程序链接起来 二.静态链接库与动态链接库 静态连接库就是把(lib)文件中用到 ...