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, 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]

原题链接:https://oj.leetcode.com/problems/combination-sum-ii/

与之前一题的差别在于每一个数字仅仅能使用一次。

故需在递归的时候直接使用下一个数字。

public class CombinationSumII {
public static void main(String[] args) {
List<List<Integer>> list = new CombinationSumII().combinationSum2(new int[]{10,1,2,7,6,1,5},8);
for(List<Integer> li : list){
for(Integer i : li)
System.out.print(i + "\t");
System.out.println();
}
}
private List<Integer> list = new ArrayList<Integer>();
private List<List<Integer>> result = new ArrayList<List<Integer>>();
public List<List<Integer>> combinationSum2(int[] num, int target) {
if(num.length == 0)
return result;
Arrays.sort(num);
dfs(num,target,0);
return result;
}
public void dfs(int[] candidates,int target,int index){
if(target < 0)
return;
if(target == 0){
result.add(new ArrayList<Integer>(list));
return;
}
for(int i=index;i<candidates.length;i++){
if(i>index && candidates[i] == candidates[i-1])
continue;
list.add(candidates[i]);
dfs(candidates,target-candidates[i],i+1);
list.remove(list.size()-1);
}
}
}

LeetCode——Combination 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] Combination Sum II 组合之和之二

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

  3. Leetcode Combination Sum II

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

  4. [LeetCode] Combination Sum II (递归)

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

  5. LeetCode Combination Sum II (DFS)

    题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...

  6. leetcode Combination Sum II python

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

  7. [Leetcode] combination sum ii 组合之和

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

  8. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

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

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

随机推荐

  1. Sutherland-Hodgeman多边形裁剪

    原文地址:http://course.cug.edu.cn/cugFirst/computer_graphics/class/course/3-3-1-a.htm

  2. OpenCV教程(45) harris角的检测(3)

          在前面一篇教程中,我们通过取局部最大值的方法来处理检测结果,但是从图像中可以看到harris角的分布并不均匀,在纹理颜色比较深的地方检测的harris角结果更密集一些.本章中,我们使用一个 ...

  3. head first--------------state pattern

    head first----------浅谈状态模式     状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类     实现代码如下:      package com.cl ...

  4. C# 遍历文件夹非递归实现(采用队列的广度优先算法)(转)

    一.实现思路: 1. 创建一个队列(使用C# 队列类 Queue,需要使用命名空间 System.Collections.Generic): 2. 把起始文件夹名称排入队中: 3. 检查队列中是否有文 ...

  5. 用Visual C#来清空回收站(2)

    四.程序的源代码(recycled.cs).编译方法及运行后的界面: (1).程序的源代码:recycled.cs: using System.IO ; using System.Windows.Fo ...

  6. multiMap遍历方法

    /* multimap中的三种遍历方法 multimap中如果没有查找到相应元素,则返回的迭代器是依据该元素的排列顺序该键应该插入的位置 如果找不到,则方法一和方法二返回的两个迭代器应该相等 */ # ...

  7. 【R】自定义函数方法

  8. Sublime 格式化代码 设置快捷键以及插件使用

    实在sublime中已经自建了格式化按钮: Edit  ->  Line  ->  Reindent 只是sublime并没有给他赋予快捷键,所以只需加上快捷键即可 Preference ...

  9. 页面显示This is the initial start page for the WebDriver server.的解决办法

    今天在做项目的时候,遇到一个奇怪的问题,打开浏览器是正常的,但是页面不会跳转到需要的URL,而是提示一行白字,如图: 反复研究了脚本,没有问题啊,但是就是不跳转. 后来查了下,在某论坛上找到了答案: ...

  10. windows命令行的使用,去掉"半"字

    1.新启动一个命令行,如果当前sogou是中文输入法,输入之后,下面出现一个"半"字.切换到英文输入法,这个"半"字,也不会消失,怎么办? 先切换到英文输入法, ...