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 ...
随机推荐
- 【UVA 401】BUPT 2015 newbie practice #2 div2-B-Palindromes
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/B A regular palindrome is a str ...
- 找回Reshaprer的Alt+Enter快捷键的方法
用过Reshaprer一段时间发现这个Visual Studio插件确实是个好东东,特别是神级快捷键Alt+Enter更是好用至极,可以解决大部分代码问题,不过会发现装上Reshaprer后VS自带的 ...
- hdu 1575 矩阵快速幂模板
#include "iostream" #include "vector" #include "cstring" using namespa ...
- foeach集合遍历
package number; public class Number { public static void main(String[] args) { int[] arr={5,2,1,0,3, ...
- C#获取本机IP且过滤非真实网卡(如虚拟机网卡)
参考了网上的文章,具体地址不记得了. 下面的方法可以过滤掉虚拟机的网卡等无效网卡,进而只留下真实的网卡. using System; using System.Collections.Generic; ...
- C++中getline被跳过
#include "stdafx.h" #include"iostream" #include"math.h" #include" ...
- soa vs cop
soa强调分层:底层为高层提供服务: cop强调分块:有明确的职责和服务提供接口,为外部提供服务. SOA 原则非常强调将服务使用者和服务提供者分离开来,关于此类分离实际的含义,有很多不正式但非常有用 ...
- asp.net在线恢复数据库
用于asp.net还原与恢复SqlServer数据库的KillSpid存储过程 CREATE PROCEDURE KillSpid(@dbName varchar(20)) AS BEGIN DECL ...
- ExtJS学习之路第八步:Window组件
一个专门Panel用作程序窗口.默认的,Window可以是浮动的(floated).可缩放(resizable)以及可拖动的(draggable).Window能够被最大化适应可视窗口,(restor ...
- POJ 1442 Black Box
第k大数维护,我推荐Treap..谁用谁知道.... Black Box Time ...