LeetCode——Combination Sum II
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的更多相关文章
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum II (递归)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- leetcode Combination Sum II python
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [Leetcode] combination sum ii 组合之和
Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- python Genarator函数
Generator函数的定义与普通函数的定义没有什么区别,只是在函数体内使用yield生成数据项即可.Generator函数可以被for循环遍历,而且可以通过next()方法获得yield生成的 数据 ...
- Go语言之进阶篇Socket编程
一.Socket编程 1.什么是Socket Socket起源于Unix,而Unix基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭clo ...
- fatal error LNK1104: 无法打开文件“libc.lib”的问题 (转)
今天,编译程序的时候,意外遇到了一个错误,就是VS2008一直提示:fatal error LNK1104: 无法打开文件“libc.lib”,后来在网上查找了很多资料,终于知道原因了... 如果将用 ...
- 五险一金 社保基数 住房公积金基数以及个税(By FlyElephant)
作为最近转正的应届毕业生,查了一下卡发现卡上的钱和工资对不上,于是自己回来研究了一下五险一金,整理如下: 什么是五险一金? 五险一金其中主要指的是养老保险,医疗保险,失业保险,工伤保险,生育保险,一金 ...
- iOS中的时钟动画
iOS 动画效果非常多,我们在开发中可能会遇到很多动画特效,我们就会用到核心动画框架CoreAnimation,核心动画里面的动画效果有很多,都是在QuartzCore.framework框架里面,今 ...
- AI-终极算法-遗传算法
- PHPExcel合并与拆分单元格
$objPHPExcel; $filepath="c:\temp.xlsx"; try { $objReader = PHPExcel_IOFactory::createRea ...
- Java归去来第2集:利用Eclipse创建Maven Web项目
一.前言 如果还不了解剧情,请返回第一集的剧情 Java归去来第1集:手动给Eclipse配置Maven环境 二.利用Eclipse创建Maven Web项目 选择File-New- ...
- OpenGL ES3 非常好的系列文章
OpenGL ES3 非常好的系列文章: OpenGL-ES 3.0学习指南(五)--EGL基础 NDK开发OpenGL ES 3.0(二)--初见GLES,第一个三角形 NDK开发OpenGL ES ...
- Win10远程桌面出现身份验证错误要求的函数不受支持
Win10更新了,远程连不上了,还是手动来修改,用户体验不好,差评! 解决方法: 在本地(而非远程机),运行 gpedit.msc,打开本地组策略:计算机配置>管理模板>系统>凭据分 ...