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

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


Combination Sum的差别就是上面红色的那行字,只要把递归时候的起点由i改为i+1即可,参见下列代码中高亮的部分:

代码如下:

 public class Solution {
private void combiDfs(int[] candidates,int target,List<List<Integer>> answer,List<Integer> numbers,int start){
if(target == 0){
answer.add(new ArrayList<Integer>(numbers));
return;
} int prev = -1; for(int i = start;i < candidates.length;i++){
if(candidates[i] > target)
break;
if(prev != -1 && prev == candidates[i])
continue; numbers.add(candidates[i]);
combiDfs(candidates, target-candidates[i], answer, numbers,i+1);
numbers.remove(numbers.size()-1); prev = candidates[i];
}
}
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> answer = new ArrayList<List<Integer>>();
List<Integer> numbers = new ArrayList<Integer>();
Arrays.sort(candidates);
combiDfs(candidates, target, answer, numbers,0); return answer; }
}

【leetcode刷题笔记】Combination Sum II的更多相关文章

  1. 【leetcode刷题笔记】Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  2. 【leetcode刷题笔记】Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. 【leetcode刷题笔记】Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  4. 【leetcode刷题笔记】N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  5. LeetCode(40) Combination Sum II

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

  6. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  7. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  8. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  9. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

随机推荐

  1. 使用Maven整合SSH总结

    本人自己进行的SSH整合,中间遇到不少问题,特此做些总结,仅供参考. 项目环境: struts-2.3.31 + spring-4.3.7 + hibernate-4.2.21 + maven-3.3 ...

  2. (十)Thymeleaf用法——Themeleaf内联

    5. 内联 [[...]]是内联文本的表示格式,但需要使用th:inline属性(分为text,javascript,none)激活. 5.1 文本内联    <p th:inline=&quo ...

  3. Doker容器之间连接

    第一个应用容器 $ sudo docker run --name=mysql_client1 --link=mysql_server:db -t -i kongxx/mysql_client /usr ...

  4. linux 进程的 5 大段

    BSS段:BSS段(bss segment)通常是指用来存放程序中数据段:数据段(data segment)通常是指用来存放程序中已初始化的全局变量的一块内存区域.数据段属于静态内存分配. 数据段:数 ...

  5. Python Windows文件操作

    获得目录和文件名 os.getenv()获取环境变量 os.putenv()设置环境变量 os.getcwd() 获得当前目录 os.chdir(‘要设置的当前目录’) os.listdir() 返回 ...

  6. E - Hangover(1.4.1)

    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...

  7. [django] 利用多线程添加异步任务

    看到django异步大家的反应应该是celery这样的消息队列组件.如今用的最多的最推荐的也是这样的方式.然而我这需求就是请求来了,运行一个小程序.可是又不能确定这个小程序啥时候运行完.响应又要及时, ...

  8. UVA 11354 - Bond (最小生成树 + 树链剖分)

    题目链接~~> 做题感悟:这题開始看到时感觉不是树不优点理,一想能够用 Kruskal 处理成树 ,然后就好攻克了. 解题思路: 先用 Kruskal 处理出最小生成树.然后用树链剖分 + 线段 ...

  9. centos root登录password 忘记解决的方法

    Centos系统 登陆root忘记password 解决方式: (1)开机启动系统,在进入linux系统之前按键Esc 进入例如以下界面:(须要注意:Centos是安装在虚拟机里面的话,须要将鼠标点进 ...

  10. HTML5 2D平台游戏开发#7Camera

    在庞大的游戏世界中,玩家不能一览地图全貌,而是只能看到其中一部分,并一步步探索,这时就要用到一种技术来显示局部的地图,游戏术语称为摄像机(Camera).下面两张图中的白色矩形框表示了Camera的作 ...