Java for LeetCode 040 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]
解题思路:
修改上题代码,将DFS宽度设置成2即可,注意使用Set,防止重复,JAVA实现如下:
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Set<List<Integer>> list = new HashSet<List<Integer>>();
Arrays.sort(candidates);
dfs(list, candidates, 0, target, 0);
return new ArrayList<List<Integer>>(list);
}
static List<Integer> list2 = new ArrayList<Integer>();
static void dfs(Set<List<Integer>> list, int[] array, int result,int target, int depth) {
if (result == target) {
list.add(new ArrayList<Integer>(list2));
return;
}
else if (depth >= array.length || result > target)
return;
for (int i = 0; i <= 1; i++) {
for (int j = 0; j < i; j++)
list2.add(array[depth]);
dfs(list, array, result + array[depth] * i, target, depth+1);
for (int j = 0; j < i; j++)
list2.remove(list2.size() - 1);
}
}
结果453 ms,效率略低,因此换掉Set,用一个变量计算每次DFS的宽度,JAVA实现如下:
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
ArrayList<List<Integer>> list = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
dfs(list, candidates, 0, target, 0);
return list;
}
static List<Integer> list2 = new ArrayList<Integer>();
static void dfs(ArrayList<List<Integer>> list, int[] array, int result,int target, int depth) {
if (result == target) {
list.add(new ArrayList<Integer>(list2));
return;
}
else if (depth >= array.length || result > target)
return;
int step=1;
while(depth<array.length-1&&array[depth]==array[depth+1]){
depth++;
step++;
}
for (int i = 0; i <= step; i++) {
for (int j = 0; j < i; j++)
list2.add(array[depth]);
dfs(list, array, result + array[depth] * i, target, depth+1);
for (int j = 0; j < i; j++)
list2.remove(list2.size() - 1);
}
}
Java for LeetCode 040 Combination Sum II的更多相关文章
- LeetCode 040 Combination Sum II
题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find al ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- leetcode 40 Combination Sum II --- java
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
随机推荐
- php复习
最近要用php,好久不用感觉手生.抓起<零基础学PHP>一书复习了下,顺带学了smarty模板语言,然后到慕课网看了些php中级视频教程,这里记录下. php最基本的文件上传 不用任何第三 ...
- LFI、RFI、PHP封装协议安全问题学习
本文希望分享一些本地文件包含.远程文件包含.PHP的封装协议(伪协议)中可能包含的漏洞 相关学习资料 http://www.ibm.com/developerworks/cn/java/j-lo-lo ...
- How to Write and Publish a Scientific Paper: 7th Edition(科技论文写作与发表教程)(11.04更新)
How to Write and Publish a Scientific Paper: 7th Edition(科技论文写作与发表教程)(11.04更新) 重要通知: 最近开题报告已差不多告一段落, ...
- spring bean id和bean name的区别
今天在分析问题时发现一个大家平时都不太注意的spring 配置问题,发出来分享下: 首先澄清一个概念: 同名bean:多个bean 有相同的 name 或者 id,称之为同名bean <bean ...
- Android Studio-AndroidStudio目录结构
最近,Google已经打算废弃Eclipse,而要大力支持他的亲生儿子AndroidStudio了,已经不在维护Eclipse了,也就是说在Eclipse上面出了什么问题,Google已经不在会管了, ...
- SqlDataReader读取分页数据,pageCount你是肿么了?
自己在折腾代码的时候发现,SqlDataReader读取分页数据,存储过程中的输出参数总页数pageCount获取不准确. 我已经问过百度,技术群等..... 都说SqlDataReader用过后关闭 ...
- C++ 模板双向不循环链表!!
CTDChain<T,sort,equal,pointer> * nodeTemp = NULL; -------------------------------------------- ...
- socket基本
fd_set用法: http://blog.sina.com.cn/s/blog_5c8d13830100erzs.htm socket连接: lpszHost="127.0.0.1&quo ...
- JVM垃圾收集策略解析
地址:http://developer.51cto.com/art/201002/184385_all.htm
- hibernate操作数据库例子
1.工程目录结构如下 2.引入需要的jar包,如上图. 3.创建持久化类User对应数据库中的user表 package com.hibernate.配置文件.pojo; import java.sq ...