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 ...
随机推荐
- 10 个免费的 jQuery 可视化编辑器插件
富文本编辑器,也就是所见即所得的 HTML 编辑器,是网站一个非常重要的组件,特别是对于一些内容发布网站来说.本文介绍 10 个基于 jQuery 的可视化文本编辑器. MarkitUp markIt ...
- url的编码问题
JQuery中 编码 var url = 'folder/index.html?param=#23dd&noob=yes'; var encodedUrl = encodeURICompone ...
- Yii2的view需要链接跳转
添加 use yii\helpers\Url; view中的连接 <?= Url::toRoute('post/index');?>//post为你的当前控制器名,index为view模版
- BIEE 创建一个简单的分析(2)
步骤: 1.如果BIEE安装在本机,直接登录http://localhost:9704/analytics/ 点击右上方导航菜单中的“新建->分析” 2.选择上节创建的RPD文件中的SCOTT主 ...
- spring - ioc和aop
1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对象的时候,一般都是直接使用关键字类new ...
- 高斯混合聚类及EM实现
一.引言 我们谈到了用 k-means 进行聚类的方法,这次我们来说一下另一个很流行的算法:Gaussian Mixture Model (GMM).事实上,GMM 和 k-means 很像,不过 G ...
- 【poj1745】 Divisibility
http://poj.org/problem?id=1745 (题目链接) 题意 给出n串数,可以在其两两之间添加+或-,判断是否存在某种方案使得出的表达式的答案可以整除k. Solution 水题一 ...
- workon在zsh中不起作用
先装了workon,然后装了zsh,发现在zsh里不起作用 翻了一下网上没有解答,就看了看bashrc文件,发现一句 source /usr/local/bin/virtualenvwrapper.s ...
- Ubuntu学习总结-01 用VMware 8安装Ubuntu 12.04详细过程
1 Ubuntu 下载地址 http://www.ubuntu.com/download/desktop 2 安装Ubuntu 转载用VMware 8安装Ubuntu 12.04详细过程 http:/ ...
- Android模拟器端口被占用问题的解决办法
一.问题描述 今天在Eclipse中运行Android项目时遇到"The connection to adb is down, and a severe error has occured& ...