Given a set of candidate numbers (C) (without duplicates) 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.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [2, 3, 6, 7] and target 7
A solution set is:

  1. [
  2. [7],
  3. [2, 2, 3]
  4. ]

题目标签:Array

  题目给了我们一个candidates array 和一个 target, 让我们从array 里找到所有的组合,它的和是等于target的。任何组合只要是它的和等于target的话,都需要找到,但是不需要重复的。这道题中是可以重复利用一个数字的,那么我们就需要每次都代入同一个数字,直到它之和达到target 或者它超过了target, 然后在倒退回去一个数字,继续找下一个数字,这种情况肯定是要用递归了。这里是backtracking,每次倒退回一个数字,需要继续递归下去,在倒退,一直重复直到搜索了所有的可能性。

  我们可以看原题中的例子:

  [2,3,6,7]  target 7

  2                                  选2,sum = 2

  2+2                              选2,sum = 4

  2+2+2                          选2,sum = 6

  2+2+2+2                      选2,sum = 8 这时候 sum已经超出target,需要返回到上一个数字

  2+2+2+3                      选3,sum = 9, 也超出了target, 这里我们可以发现,如果是sorted array的话,从小到大,只要一次超出,后面的数字必然也会超出target,所以可以在第一次超出的时候就直接跳出这一个迭代

  2+2+3                          选3,sum = 7,等于target, 此时返回并且跳出这一个迭代,因为后面的数字肯定超出(array里不会有重复的数字)

  2+3                              选3,sum = 5,小于target,继续递归同一个数字

  2+3+3                          选3,sum = 8,超出target,返回上一个数字

  2+6                              选6,sum = 8,超出target,返回上一个数字

  3                                  选3,这里继续从3开始递归

  ...

  ...

  ...

  我们可以看出,一开始有一个迭代从2,一直到7,然后把每一个数字递归下去,包括它自己,每次递归下去的数字,会继续有一个迭代,一旦超出或者等于了,返回前面一个数字继续递归。所以把array sort一下就可以提早跳出那一轮的迭代。

具体看下面代码。

Java Solution:

Runtime beats 97.14%

完成日期:07/16/2017

关键词:Array

关键点:Backtracking with sorted array

  1. public class Solution
  2. {
  3. public List<List<Integer>> combinationSum(int[] candidates, int target)
  4. {
  5. List<List<Integer>> list = new ArrayList<>();
  6. Arrays.sort(candidates);
  7. backtrack(list, new ArrayList<>(), candidates, target, 0);
  8.  
  9. return list;
  10. }
  11.  
  12. public static boolean backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int remain, int start)
  13. {
  14. if(remain < 0) // if remain is 0 or less than 0, meaning the rest numbers are even greater
  15. return false; // therefore, no need to continue the loop, return false
  16. else if(remain == 0)
  17. {
  18. list.add(new ArrayList<>(tempList));
  19. return false;
  20. }
  21. else
  22. {
  23. for(int i=start; i<nums.length; i++)
  24. {
  25. boolean flag;
  26. tempList.add(nums[i]);
  27. flag = backtrack(list, tempList, nums, remain - nums[i], i); // not i + 1 because we can use same number.
  28. tempList.remove(tempList.size() - 1);
  29.  
  30. if(!flag) // if find a sum or fail to find a sum, there is no need to continue
  31. break;// because it is a sorted array with no duplicates, the rest numbers are even greater.
  32. }
  33.  
  34. return true; // return true because previous tempList didn't find a sum or fail a sum
  35. }
  36. }
  37. }

参考资料:

https://leetcode.com/problems/combination-sum/#/discuss

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 39. Combination Sum (组合的和)的更多相关文章

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

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

  2. [leetcode]39. Combination Sum组合之和

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

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

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

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

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

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

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

  6. 【LeetCode】Combination Sum(组合总和)

    这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...

  7. leetcode 39 Combination Sum --- java

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

  8. Java [Leetcode 39]Combination Sum

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

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

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

随机推荐

  1. 【Socket编程】通过Socket实现UDP编程

    通过Socket实现UDP编程 UDP通信: 1.UDP协议(用户数据报协议)是无连接.不可靠.无序的. 2.UDP协议以数据报作为数据传输的载体. 3.使用UDP进行数据传输时,首先需要将要传输的数 ...

  2. Project Euler:Product-sum numbers (problem 88) C++

    A natural number, N, that can be written as the sum and product of a given set of at least two natur ...

  3. ActiveMQ_Windows版本的安装部署

    1, 保证电脑上安装了jdk6以上版本的java,并配置了好环境变量 : 2, 官方下载地址:http://activemq.apache.org/download-archives.html ,这里 ...

  4. 关于Linux的loop设备

    偶然发现/dev目录里有几个loop设备,一番搜索得知,这是一种伪设备(pseudo-device),它可以把一个文件连接为设备(就像Windows下用虚拟光驱挂载ISO文件). 遂做实验验证: 1. ...

  5. String类的一些常见的获取方法(5)

     String s = "aasfasfdtgsrast"; 1: int a = s.length() //返回字符串的长度 2: char s1 = charAt(int in ...

  6. Https系列之三:让服务器同时支持http、https,基于spring boot

    Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...

  7. Visual Studio + Qt开发环境搭建

    1. 安装Visual Studio 2015 Visual Studio 2015下载地址如下,安装比较常规,不做介绍. Visual Studio Enterprise 2015 with Upd ...

  8. Linux crontab任务调度

    一.crontab说明 Linux crontab任务调度是在规定的时间频率内去执行相应的任务. 二.crontab文件详情 1.crontab文件在Linux中的/etc/crontab 2.查看c ...

  9. Another Easy Problem fzu1753

    Another Easy Problem Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u ...

  10. Ubuntu 16.04源码编译安装nginx 1.10.0

    一.下载相关的依赖库 pcre 下载地址 http://120.52.73.43/jaist.dl.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.t ...