题目描述:

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中,如果加入的数目太多导致下面没有数字可以使综合等于target则将之前加入的数字弹出,换进下一个数字,如此反复。

代码如下:

public class Solution {
public List<List<Integer>> combinationSum(int[] candidates,
int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<List<Integer>>();
getResult(result, new ArrayList<Integer>(), candidates, target, 0);
return result;
} public void getResult(List<List<Integer>> result,
List<Integer> current, int[] candiates, int target, int start) {
if (target > 0) {
for (int i = start; i < candiates.length && target >= candiates[i]; i++) {
current.add(candiates[i]);
getResult(result, current, candiates, target - candiates[i], i);
current.remove(current.size() - 1);
}
} else if (target == 0) {
result.add(new ArrayList<Integer>(current));
}
}
}

  

Java [Leetcode 39]Combination Sum的更多相关文章

  1. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  2. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  3. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  4. leetcode 39 Combination Sum --- java

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

  5. LeetCode 39. Combination Sum (组合的和)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  6. LeetCode 39 Combination Sum(满足求和等于target的所有组合)

    题目链接: https://leetcode.com/problems/combination-sum/?tab=Description   Problem: 给定数组并且给定一个target,求出所 ...

  7. Java [Leetcode 40]Combination Sum II

    题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...

  8. [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...

  9. Leetcode 39. Combination Sum

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

随机推荐

  1. Django文档——Model字段选项(Field Options)

    建立一个简易Model class Person(models.Model): GENDER_CHOICES=( (1,'Male'), (2,'Female'), ) name=models.Cha ...

  2. mysqlsla慢查询分析工具教程

    mysqlsla是一款帮助语句分析.过滤.和排序的功能,能够处理MySQL慢查询日志.二进制日志等.整体来说, 功能非常强大. 能制作SQL查询数据报表,分析包括执行频率, 数据量, 查询消耗等. 且 ...

  3. JAVA中的各种 哈希码(HashCode) 与 equals方法在HIBERNATE的实际应用[转载]

    1.什么是哈希码(HashCode) 在Java中,哈希码代表对象的特征.例如对象 Java代码 String str1 = “aa”, str1.hashCode= 3104 String str2 ...

  4. iOS7.1Https企业证书发布方法

    openssl使用的是macos系统自带的版本,关键点是不同直接使用ios设备打开https的链接,需要将证书发到系统的mail里,安装到设备, 如果命令执行不成功,用sudo执行. 1.生成服务器的 ...

  5. jvm 参数调优

    FROM: http://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html#CMSInitiatingOccupancyFraction ...

  6. webstorm 11 安装配置 grunt 时遇到的问题及解决办法

    想学grunt的可以看看这篇文章,写的很有意思,教程之类的我就不写了,网上很多资料,我就记录下我遇到的问题和解决办法. http://yujiangshui.com/grunt-basic-tutor ...

  7. js解决checkbox全选和反选的问题

    function SelectAll() { var checkboxs=document.getElementsByName("chk_list"); for (var i=0; ...

  8. [转载]深入理解ASP.NET MVC之ActionResult

    Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...

  9. [转载]自定义ASP.NET MVC Html辅助方法 TagBuilder

    在ASP.NET MVC中,Html辅助方法给我们程序员带来很多方便,其重要性也就不言自明.有时候,我们不想重复地写一些HTML代码,或者MS没有提供我们想要的那个HTML标签的Html辅助方法,那么 ...

  10. API断点大全

    1.限制程序功能函数 EnableMenuItem 允许.禁止或变灰指定的菜单条目EnableWindow 允许或禁止鼠标和键盘控制指定窗口和条目(禁止时菜单变灰) 2.对话框函数 CreateDia ...