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 ...
随机推荐
- java cglib动态代理原理及样例
cglib动态代理: http://blog.csdn.net/xiaohai0504/article/details/6832990 一.原理 代理为控制要访问的目标对象提供了一种途径.当访问 ...
- 2013成都网赛 G(x) (HDU 4733)
G(x) 思路: 首先搞清楚每个位置上的值有什么意义, 如果第i位的值为1则 第i位与第i+1位不同,反之相同. 然后考虑s1和s2为什么会不一样, 这是由于x+1后比特位进位导致的,于是得出一个性质 ...
- Linux下用命令格式化U盘
1.找到U盘位置(已挂载) sudo fdisk -l 如图,我的在/dev/sdc4 2.格式化U盘 sudo mkfs -t vfat /dev/sdc4 -t 后面是格式化为哪种文件系统格式,v ...
- CRC 模式及实现
CRC : Cyclic redundancy Check 循环冗余校验 概述参见wiki百科:http://en.wikipedia.org/wiki/Cyclic_redundancy_check ...
- SQLLoader3(数据文件没有分隔符时的导入)
数据文件:D:\oracletest\ldr_tab_fiile.dat1.数据文件字段中间以制表符TAB隔开:7369 SMITH CLERK7499 ALLEN SALESMAN7521 WARD ...
- SQLServer 跨服务器查询的两个办法
网上搜了跨服务器查询的办法,大概就是Linked Server(预存连接方式并保证连接能力)和OpenDataSource(写在语句中,可移植性强).根据使用函数的不同,性能差别显而易见...虽然很简 ...
- 如何利用 Bootstrap 进行快速 Web 开发
原文出处: IBM developerworks 了解如何使用 Bootstrap 快速开发网站和 Web 应用程序(包括移动友好型应用程序).Bootstrap 以 LESS 项目为基础,由 Twi ...
- ms sql 根据表名查询 表中所有字段的属性值 sql语句
SELECT表名=case when a.colorder=1 then d.name else '' end,--表说明=case when a.colorder=1 then isnull(f.v ...
- select选项改变时获取选中的option的值
本来天真的以为直接this.value()就能去到select的值,可事实并非那么回事. <script> document.getElementById('select').onchan ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...