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. exec 和 spawn 的区别

    参考资料: difference-between-spawn-and-exec-of-node-js-child_process process_child 最近在用nodejs 的child_pro ...

  2. linux安装与卸载软件

    在ubuntu系统中,通常使用apt-get命令完成对软件的安装与卸载 安装的软件通常都放置在一些源中,国内有很多镜像源供下载使用,而系统设置的源保存在目录/etc/apt/sources.list文 ...

  3. 书写优雅的shell脚本(八)- 日期格式化

    1. 将日期格式转为时间戳 获取当前时间:currenttime=`date "+%Y-%m-%d %H:%M:%S"` 结果:2015-04-13 11:15:43 将当前时间转 ...

  4. 「NOIP2006」「LuoguP1064」 金明的预算方案(分组背包

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NNN元钱就行” ...

  5. python爬虫知识点总结(五)正则表达式

    在线正则表达式匹配:http://tool.oschina.net/regex 正则表达式学习:https://c.runoob.com/front-end/854 一.什么是正则表达式? 常见匹配模 ...

  6. poj2299——逆序对

    题目:http://poj.org/problem?id=2299 逆序对,注意树状数组维护后缀和. 代码如下: #include<iostream> #include<cstdio ...

  7. JavaScript的中类型转换

    JavaScript的类型转换 By 大志若愚  (一)转换为字符串 X + '' toString() String() 函数转换为字符串一般是将函数体输出,不过可以重写其toString方法  ( ...

  8. LINUX socket网络编程

    1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在自己的地址范围内运行,为保证两个相互通信的进 程之间既互不干扰又协调一致工作,操作系统为进程通信提供了相应设施,如 U ...

  9. 【win10激活问题】 从【win10专业工作站版】转为 数字许可证的【win10专业版】

    今天安装了 win10 1903 (10.0.18362 暂缺 Build 18362) 安装时 选的 是[win10 专业工作站版] 却无法激活, (因为当初是从win7升级上win10的,只有关联 ...

  10. shell expr 的使用注意事项

    #!/bin/bash a=10 b=20 c=`expr $a + $b` echo "a + b :$c" c='expr $a + $b' echo "a + b ...