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. “libgomp.so.1: version `GOMP_4.0' not found” || “libstdc++.so.6: version `CXXABI_1.3.8' not found”错误

    类似问题还有 'ImportError ../lib/libstdc++.so.6: version `CXXABI_1.3.7' not found (required by xxx)'.      ...

  2. poj1475 Pushing Boxes[双重BFS(毒瘤搜索题)]

    地址. 很重要的搜索题.★★★ 吐槽:算是写过的一道码量比较大的搜索题了,细节多,还比较毒瘤.虽然是一遍AC的,其实我提前偷了大数据,但是思路还是想了好长时间,照理说想了半小时出不来,我就会翻题解,但 ...

  3. JavaScript-Tool:three.js

    ylbtech-JavaScript-Tool:three.js Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包括了摄影机.光影.材质等各种对象.你可以在它的主页 ...

  4. CentOS虚拟机通过主机WIFI上网

    0 环境简介 环境如下: (1)宿主机为WIN7系统,连接内网,同时通过本机的WIFI上外网: (2)虚拟机为VMWare12下的CentOS7系统. 1 虚拟机设置 选择NAT 模式: 2 宿主机W ...

  5. [hdu3078]Network(LCA+排序)

    题意:维护树上两点之间的最短路径,其一,将点a的值变为b,其二,求路径上第k大的值. 解题关键:LCA+sort 复杂度:$O(qn\log n + n\log n)$ 数据弱不怪我 //#pragm ...

  6. CodeForces 485A Factory (抽屉原理)

    A. Factory time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  7. Guid.NewGuid().ToString()的几种格式 (转)

    1.Guid.NewGuid().ToString("N") 结果为:      38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid() ...

  8. win form treeview添加节点

    //实例代码: /// <summary> /// 添加节点事件 /// </summary> /// <param name="tnodes"> ...

  9. python集合介绍

    set原理 Python 还 包 含 了 一 个 数 据 类 型—— set ( 集 合 ) . 集 合 是 一 个 无 序 不 重 复 元素 的 集 . 基 本 功 能 包 括 关 系 测 试 和 ...

  10. AI决策算法 之 GOAP (二)

    http://blog.csdn.net/lovethRain/article/details/67634803 GOAP 的主要逻辑: 1.Agent的状态机初始化Idle状态 2.Idel状态根据 ...