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 ...
随机推荐
- Sublime Text 3中SublimeLinter的使用
关于Sublime Text 2中的SublimeLinter的使用网上多如牛毛,基本上不会遇到什么问题,简单的讲一下关于Sublime Text 3中遇到的问题: 1.通过package cont ...
- 【Nodejs】使用http.request批量下载MP3,发现网络文件大于1000K时下载文件为0K
这又一次让我对http.request产生质疑 //====================================================== // 喜爱123四年级上英语MP3下载 ...
- 安装ecshop2.7时候的错误处理 php版本不兼容引起
装ECShop2.7.3出现了一堆问题,主要是因为PHP版本过高引起的,不愿意降低版本,则只能一个个解决啦!这些问题包括:preg_replace.cls_image::gd_version.end( ...
- Wifidog协议V1
Wifidog网关协议V1 网关心跳(Ping协议) Wifidog将ping协议作为心跳机制向认证服务器发送当前状态信息.这可以实现为认证服务器每个节点的状态生成中央日志. Wifidog客户端在c ...
- Android 高级 Jackson Marshalling(serialize)/Unmarshalling(deserialize)
本文内容 高级 Jackson Marshalling 只序列化符合自定义标准的字段 把 Enums 序列化成 JSON 对象 JsonMappingException(没有找到类的序列化器) Jac ...
- Android 基本 Jackson Marshalling(serialize)/Unmarshalling(deserialize)
本文内容 基本 Jack Marshalling 忽略属性 忽略 Null 字段 改变字段名字 基本 Jackson Marshalling 把 JSON 解析成 JsonNode Unmarshal ...
- java报错综合
1.java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I 方法一:这是因为在struts2自带的antlr-2.7.2.jar包的 ...
- MySql 比Replace Into更适合的用法,外加SqlServer的方式。
Mysql: INSERT INTO `his_examine_result` (Mid,His_Examine_Mid, His_File_Mid, ResultType, His_Employee ...
- Java 代码行统计(转)
package codecounter; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFound ...
- python globals和locals
文章里面说globals和locals函数返回的是命名空间 - 一个存有对应作用域的所有的变量.方法的字典,注意这里和dir函数返回数组的不一样 Python命名空间的本质 class Test(ob ...