Combination Sum II 解答
Question
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]
Solution 1 -- DFS
Similar solution as Combination Sum.
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i = 0; i < candidates.length; i++) {
List<Integer> record = new ArrayList<Integer>();
record.add(candidates[i]);
dfs(candidates, i, target - candidates[i], result, record);
}
return result;
}
private void dfs(int[] candidates, int start, int target, List<List<Integer>> result, List<Integer> record) {
if (target < 0)
return;
if (target == 0) {
if (!result.contains(record))
result.add(new ArrayList<Integer>(record));
return;
}
// Because C can only be used once, we start from "start + 1"
for(int i = start + 1; i < candidates.length; i++) {
record.add(candidates[i]);
dfs(candidates, i, target - candidates[i], result, record);
record.remove(record.size() - 1);
}
}
}
Solution 2 -- BFS
To avoid duplicates in array, we can create a class:
class Node {
public int val;
public int index;
}
And put these node in arraylists for BFS.
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 ...
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
- [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 ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 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 & Combination Sum II & Multiply Strings
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...
- leetcode-combination sum and combination sum II
Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...
随机推荐
- (DP)Best Time to Buy and Sell Stock
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- LeetCode198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- python threading基础学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' """ python是支持多线程的,并 ...
- 使用 Microsoft.ApplicationBlocks.Data SqlHelper 查询超时以及解决方案
提示: 后面附有文件,不喜欢看吐槽的,直接到文章结尾下载 摘要:Data Access Application Block 是一个 .NET 组件,包含优化的数据访问代码,可以帮助用户调用存储过程以 ...
- execlp函数使用
原文:http://blog.sina.com.cn/s/blog_6a1837e901011167.html execlp(从PATH 环境变量中查找文件并执行) 相关函数: fork,ex ...
- 公司项目笔记-导出excel
一.asp.net中导出Excel的方法: 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出流写给浏览 ...
- Code Complete 读后总结和新的扩展阅读计划
Code Complete 读后总结和新的扩展阅读计划 用了一年时间终于将代码大全读完了,在这里做一个简单的总结,并安排下一阶段的扩展阅读计划. 1.选择代码大全作为我程序员职业入门的第一本书,我认为 ...
- varchar和Nvarchar的区别
(1)varchar(N) 存储时 N的单位是字节 比如说varchar(2) 代表的是 该字段可以存储字节长度为2的数据 例子:可以添加 张 或者 ab 添加成功! 但添加的时候如果是: ...
- asp.net缓存(三)
Asp.net应用程序数据缓存 System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类.此命名空间包括 Cache 类,该类是一个字典,您可以在其中存储任意数据对象,如哈希表和 ...
- 502 Proxy Error。The ISA Server denied the specified Uniform Resource Locator (URL).
问题:部署好项目,在IE地址栏输入http://localhost/myweb/index.aspx,回车后报错: 解释: 试图访问的页面出现问题,无法显示此页面. 尝试下列: 刷新页: 单击“刷 ...