Combination Sum 解答
Question
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
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 2,3,6,7 and target 7,
A solution set is: [7] [2, 2, 3]
Solution 1 -- BFS
We can also draw the solution tree. For example, input is [2,3,6,7] and 22
[]
/ / \ \
[2] [3] [6] [7]
/ / \ \ / \ \ \ \ \
[2] [3] [6][7] [3][6][7] [6][7] [7]
.............................................................
We can find silimar regulation as Problem Subsets
Difference is here when we find that current sum of list is greater than target number, we will not add it to next array.
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<List<Integer>> current = new ArrayList<List<Integer>>();
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int length = candidates.length;
for (int i = 0; i < length; i++) {
List<Integer> list = new ArrayList<Integer>();
list.add(candidates[i]);
if (target == candidates[i])
result.add(list);
if (target > candidates[i])
current.add(list);
map.put(candidates[i], i);
}
while (current.size() > 0) {
List<List<Integer>> next = new ArrayList<List<Integer>>();
int l = current.size();
for (int i = 0; i < l; i++) {
List<Integer> tmp = current.get(i);
int ll = tmp.size();
int last = tmp.get(ll - 1);
int index = map.get(last);
// Sum up current list
int total = 0;
for (int j = 0; j < ll; j++)
total += tmp.get(j);
for (int j = index; j < length; j++) {
if (total + candidates[j] < target) {
List<Integer> newList = new ArrayList<Integer>(tmp);
newList.add(candidates[j]);
next.add(newList);
} else if (total + candidates[j] == target) {
List<Integer> newList = new ArrayList<Integer>(tmp);
newList.add(candidates[j]);
result.add(newList);
}
}
}
current = next;
}
return result;
}
}
Solution 2 -- DFS
This also can be solved by DFS. End criterion is leftTarget <= 0.
Refer to this blog, we have two ways to check duplicated solutions.
1. if(i>0 && candidates[i] == candidates[i-1])//deal with dupicate
continue;
2. if(!res.contains(item))
res.add(new ArrayList<Integer>(item));
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i = 0; i < candidates.length; i++) {
dfs(candidates, target, i, result, new ArrayList<Integer>());
}
return result;
}
private void dfs(int[] nums, int target, int start, List<List<Integer>> result, List<Integer> list) {
if (target < 0)
return;
if (target == 0) {
// Avoid duplicated solutions
if (!result.contains(list))
result.add(new ArrayList<Integer>(list));
return;
}
for (int i = start; i < nums.length; i++) {
list.add(nums[i]);
dfs(nums, target - nums[i], i, result, list);
list.remove(list.size() - 1);
}
}
}
Combination Sum 解答的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Combination Sum II 解答
Question Given a collection of candidate numbers (C) and a target number (T), find all unique combin ...
- Combination Sum系列问题
主要使用方法是backtracking. Combination Sum Given a set of candidate numbers (C) and a target number (T), f ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 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 ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
随机推荐
- Spring MVC Controller 单元测试
简介 Controller层的单元测试可以使得应用的可靠性得到提升,虽然这使得开发的时间有所增加,有得必失,这里我认为得到的比失去的多很多. Sping MVC3.2版本之后的单元测试方法有所变化,随 ...
- linux网络编程之TCP/IP基础
(一):TCP/IP协议栈与数据包封装 一.ISO/OSI参考模型 OSI(open system interconnection)开放系统互联模型是由ISO(International Organi ...
- MVC Razor视图引擎
Razor 不是编程语言.它是服务器端标记语言. Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法 当网页被写入浏览器时,基于服务器的代码能够创建动 ...
- web前端之 HTML标签详细介绍
html标签的分类 点我查看完整的html标签介绍 在html中,标签一般分为块级标签和行内标签 块级标签:块元素一般都从新行开始,它可以容纳内联元素和其他块元素,常见块元素是段落标签"p& ...
- ios打包应用程序,生成ipa文件
假设我的程序调试好了,怎么才干发给别人用呢?正常情况下IPA文件是从Xcode的Organizer中输出的,可是我们没有证书,这样输出会产生错误. 以下教你怎样生成ipa文件: 1.到你当前proje ...
- 1. Git 克隆代码
1. Git 克隆代码 git clone git://github.com/facebook/hiphop-php.git 2. Git更新分支 查看服务器上的所有分支 [huzg@slave3 h ...
- 大到可以小说的Y组合子(二)
问:上一回,你在最后曾提到"抽象性不足",这话怎么说? 答:试想,如果现在需要实现一个其它的递归(比如:Fibonacci),就必须把之前的模式从头套一遍,然后通过fib_make ...
- js中的eval方法转换对象时,为何一定要加上括号?
待转换的是一个Json字符串: {'name':'新欢'} 而使用如下这种方式调用则会抛出语法异常, eval("{'name':'新欢'}"); 必须加上括号才行 eval(&q ...
- C# 实现预览dwg文件完整源代码(无需autocad环境)
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...
- MIT scheme入门使用
在win7下可安装MIT-GUN scheme, 点开后有两个界面:一个交互式命令行界面:一个Edwin界面. 在命令行界面按Ctrl-G可以开始输入.在Edwin界面,输入完整命令后按Ctrl ...