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]

题目大意:给一个候选数组,一个目标值,从候选数组找出所有和等于目标值的List,候选数组元素可以重复。

解题思路:还是用回溯的方式来做,因为可以重复,所以每次都可以从当前元素开始往后依次添加到List,递归判断,然后回溯。

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return res;
}
List<Integer> tmp = new ArrayList<>();
helper(res, tmp, 0,candidates, target);
return res;
} private void helper(List<List<Integer>> res, List<Integer> tmp, int start,int[] candidates, int target) {
if (0 == target) {
res.add(new ArrayList<>(tmp));
// System.out.println(tmp);
return;
}
for (int i = start; i < candidates.length && target >= candidates[i]; i++) {
tmp.add(candidates[i]);
helper(res, tmp, i,candidates, target - candidates[i]);
tmp.remove(tmp.size() - 1);
int ca = candidates[i];
while(i<candidates.length&&ca==candidates[i]){
i++;
}
}
}

Combination Sum —— LeetCode的更多相关文章

  1. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

  2. Combination Sum [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/combination-sum/ Basic idea: It seems complicat ...

  3. Combination Sum leetcode java

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...

  4. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)

    根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...

  6. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. 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 ...

随机推荐

  1. C# 内存管理优化畅想(三)---- 其他方法&结语

    前两篇文章提出的优化方法,都是不需要修改源代码的,而是在CLR或JIT层面进行自动优化的.但本文中提出的优化方法则需要引入新的语法,开发者只有在源代码中使用了这些新语法,才会获得优化. 1. 允许对象 ...

  2. C# 内存管理优化畅想(一)---- 大对象堆(LOH)的压缩

    我们都知道,.net的GC是不会压缩大对象堆的,因为其时间开销不可接受,但这是以大对象堆产生大块碎片为代价的,如果以后要分配的大对象比最大的碎片还大,那么即使它比所有碎片的总大小要小,也是无法在不扩展 ...

  3. 绝对炫的幻灯片插件-SKITTER

    绝对炫的幻灯片插件-SKITTER 所属分类:媒体-幻灯片和轮播图,图片展示,滑块和旋转 Includes code source // Styles <link href="css/ ...

  4. 使用Spring简化JDBC操作数据库

    Spring的开发初衷是为了减轻企业级开发的复杂度,其对数据库访问的支持亦如此,使用Spring访问数据库能带来以下好处: 1.1     简化代码 使用原生的JDBC访问数据库,一般总是要执行以下步 ...

  5. WisDom.Net 框架设计(二) 服务总线

    WisDom.Net 框架设计--服务总线 1.Soa 简介     soa 就是面向服务的体系结构 是一个组件模型,不同的组件之间通过定义良好的接口联系起来.就像盖房子一块砖头一块砖头的砌墙,一片一 ...

  6. 在xcode6.1和ios10.10.1环境下实现app发布

    之前写过在xcode6.1和ios10.10.1环境下实现真机测试,以及最近提交的app一直在审核当中,所以木有发布如何实现app发布来分享给大家.刚好昨天app审核通过了,所以就分享一篇如何实现ap ...

  7. Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志

    一.spring aop execution表达式说明 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义 ...

  8. spring jdbctemplate调用procedure(返回游标)

    package cn.com.git.htsc.uac.core.repository.report; import cn.com.git.htsc.uac.core.api.dto.report.R ...

  9. zoj 3209.Treasure Map(DLX精确覆盖)

    直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> ...

  10. JQuery 实现鼠标经过图片高亮显示,其余图片变暗

    效果图: 当鼠标经过图片时,其余图片变暗,来高亮显示当前图片,主要用的是对比度.当然你也可以先把其他图片默认变暗,鼠标经过时高亮显示,不过,无鼠标经过时整体图片都会是偏暗色调. 效果可以通过 三步实现 ...