1、问题描述

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:

[
[7],
[2, 2, 3]
] 2、边界条件:无
3、思路:先取一个数,然后与target比较;==则存贮,!=则继续从所有数里面选择。
  从Level-N里面选择一个数,与目标比较,符合则存贮;不符合则再从所有数里面挨个取,从而化为同样的Level-N问题
形成递归。base case:1)与目标匹配;2)target - nums[i]<0,再减下去也是负数,这依赖于题目给的 全正数positive integers 条件
4、实现代码:

class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> results = new ArrayList<>();
//Arrays.sort(candidates);
combinationSum(results, new ArrayList<Integer>(), candidates, target);
return results;
} public void combinationSum(List<List<Integer>> results, List<Integer> cur, int[] candidates, int target) {
if (0 == target) {
       /**
Collections.sort(cur);//这里排序会把原列表改变,所以上层在恢复现场时出错。
if (!results.contains(cur)) {//去重
results.add(new ArrayList<Integer>(cur));
}
       **/

        ArrayList<Integer> result = new ArrayList<Integer>(cur);//先生成新的cur,然后进行排序
        Collections.sort(result); //
        if (!results.contains(result)) {
          results.add(result);
               return;

        }
if (0 > target) {
return;
}
for (int i = 0; i < candidates.length; i++) {
cur.add(candidates[i]);
combinationSum(results, cur, candidates, target - candidates[i]);
cur.remove(cur.size() - 1);
}
}
}

5、时间复杂度:说不好; 空间复杂度:

6、题外知识:Arraylist排序:Collections静态排序API,Collections的排序都是稳定的。Collections.sort(List<T> list)、和Collections.sort(List<T> list,Comparator<?super T> c);使用的排序是稳定的,主要是对list排序。

链接:http://blog.csdn.net/tuke_tuke/article/details/51100219 和 http://www.importnew.com/17211.html  
7、优化解法
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> results = new ArrayList<>();
Arrays.sort(candidates);//排序是为了使得答案有序;如果有重复数字的情况下,可以方便去重。
combinationSum(results, new ArrayList<Integer>(), candidates, target, 0);
return results;
} public void combinationSum(List<List<Integer>> results, List<Integer> cur, int[] candidates, int target, int start) {
if (0 == target) {
results.add(new ArrayList<Integer>(cur));
return;
}
if (0 > target) {
return;
}
for (int i = start; i < candidates.length; i++) { ///从start开始是因为前面的数字已经遍历过自己和后面的数字。
cur.add(candidates[i]);
combinationSum(results, cur, candidates, target - candidates[i], i);// not i + 1 because we can reuse same elements
cur.remove(cur.size() - 1);
}
}
}
 

 

leecode-39. Combination Sum的更多相关文章

  1. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

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

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

  3. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

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

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

  5. 39. Combination Sum - LeetCode

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

  6. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

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

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

  8. 【LeetCode】39. Combination Sum (2 solutions)

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  9. LeetCode笔记:39. Combination Sum

    题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...

  10. 39. Combination Sum

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

随机推荐

  1. codeforces 558B B. Amr and The Large Array(水题)

    题目链接: B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes in ...

  2. 运算符-----------instanceof

  3. 【LeetCode】091. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  4. 关于UML图的生成

    想把一个java工程生成UML图非常简单,之前我的eclipse是4.2.0的,没有对应的GEF,所以我索性就直接把工程粘到了My Eclipse中,因为My Eclipse里面有UML自动生成的功能 ...

  5. [PE182]RSA encryption

    https://projecteuler.net/problem=182 题意: 找出满足下列条件的所有$e$ 的和, - $1 < e < \varphi \left( {1009,36 ...

  6. VMware10中安装centos7没有可用的网络设备

    1.问题描述 centos7安装到虚拟机无法上网   2.安装环境 win7 x64  WM 10.01 iso  CentOS-7-x86_64-DVD-1503-01.iso  {4.01G}   ...

  7. FZU2056 最大正方形(二分答案)

    Problem 2056 最大正方形 Accept: 171    Submit: 516Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  8. 利用msfvenom制作木马程序(你可以得到她的一切)

    实验环境: 虚拟机:kali  (攻击机)          192.168.1.2 虚拟机:windwos 2003 (靶机)   192.168.1.100 1. 制作木马 说明: -p payl ...

  9. php中使用mysqli和pdo扩展,测试mysql数据库的执行效率。

    <?php /** * 测试pdo和mysqli的执行效率 */ header("Content-type:text/html;charset=utf-8"); //通过pd ...

  10. DeleteDC ReleaseDC DeleteObject之间的区别

    DeleteDC 该函数删除指定的设备上下文环境(DC). 原型: BOOL DeleteDC(HDC hdc): 参数: hdc:设备上下文环境的句柄. 返回值: 成功,返回非零值:失败,返回零.调 ...