39 Combination Sum

DFS处理，有两个比较坑的要求：

• Elements in a combination (a1a2, … , 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

onstack法搜索所有的可能会导致同时出现[2,2,3]、[3,2,2]这样的解，没有顺序？就加顺序

public class Solution {
List<List<Integer>> retlist = new ArrayList<>(512);

public List<List<Integer>> combinationSum(int[] candidates, int target)
{

Arrays.sort(candidates);

dfs(candidates, target, 0, -1, new ArrayList<Integer>());

return retlist;
}

public void dfs(int[] arr, int t, int sum, int lastindex,ArrayList<Integer> arraylist)
{
if (sum > t)
return;
if (sum == t)
{
return;
}

for (int i = 0; i < arr.length; i++)
{
if (i >= lastindex)
{
dfs(arr, t, sum + arr[i], i, arraylist);
arraylist.remove(arraylist.size()-1);
}

}
}
}

leetcode 39. Combination Sum-回溯算法|递归|非递归

2016-03-15 17:37:26

[LeetCode39]Combination Sum

2014-06-17 04:15:23

回溯详解及其应用：Leetcode 39 combination sum

2017-01-31 16:47:37

Leetcode #39. Combination Sum 组合求和 解题报告

2016-04-10 23:42:47

[LeetCode]39.Combination Sum

2015-01-27 17:47:03

leetCode 39.Combination Sum(组合总和) 解题思路和方法

2015-07-09 22:33:11

LeetCode39:Combination Sum

2015-07-06 20:58:02

回溯法——combination-sum、combination-sum-ii

2016-05-31 13:27:11

LeetCode39——Combination Sum

2015-10-17 21:08:58

leetcode【39+40+216+377 Combination Sum 相关】【python】

2016-12-13 16:38:48