Combination Sum

Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution
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]

SOLUTION 1:

经典递归模板。

i 的起始值是跟排列的最主要区别。因为与顺序无关,所以我们必须只能是升序,也就是说下一个取值只能是i本身或是i的下一个。
但是排列的话,可以再取前同的。1, 2 与2 1是不同的排列,但是同一个组合

同学们可以看下这几个题目的区别。

LeetCode: Letter Combinations of a Phone Number 解题报告

LeetCode: Permutations II 解题报告

LeetCode: Permutations 解题报告

LeetCode: Combinations 解题报告

 public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (candidates == null || candidates.length == 0) {
return ret;
} // Sort to avoid duplicate solutions.
Arrays.sort(candidates); dfs(candidates, target, new ArrayList<Integer>(), ret, 0);
return ret;
} public void dfs(int[] candidates, int target, List<Integer> path, List<List<Integer>> ret, int index) {
if (target < 0) {
return;
} if (target == 0) {
ret.add(new ArrayList(path));
return;
} // i 的起始值是跟排列的最主要区别。因为与顺序无关,所以我们必须只能是升序,也就是说下一个取值只能是i本身
// 或是i的下一个。
// 但是排列的话,可以再取前同的。1, 2 与2 1是不同的排列,但是同一个组合
for (int i = index; i < candidates.length; i++) {
int num = candidates[i];
path.add(num); // 注意,最后的参数是i,不是index!!
dfs(candidates, target - num, path, ret, i);
path.remove(path.size() - 1);
}
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/combination/CombinationSum_1203.java

LeetCode: Combination Sum 解题报告的更多相关文章

  1. 【LeetCode】39. Combination Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...

  2. LeetCode: Path Sum 解题报告

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  6. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. [LeetCode] Combination Sum 组合之和

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

  8. [Leetcode] Combination Sum 系列

    Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...

  9. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. CentOS 6.4安装配置LAMP服务器

    CentOS 6.4安装配置LAMP服务器(Apache+PHP5+MySQL) 作者: 字体:[增加 减小] 类型:转载 这篇文章主要介绍了CentOS 6.4安装配置LAMP服务器(Apache+ ...

  2. python3.x 和 python2.x关于 urllib的用法

    在python2.x版本中可以直接使用import urllib来进行操作,但是python3.x版本中使用的是import urllib.request来进行操作,下面是简单的例子: python2 ...

  3. virtualenv沙箱

    准备用Django开发网站,豆瓣阅读中有<Django入门>,里面有人批注说用virtualenv+pip很好. 每次搞个什么都感觉举步维艰,因为之前太懒了,什么都懒得深入看一下.pip一 ...

  4. ASP.NET#使用母版时,如果要使用js中的getElementById()方法取得某个内容页的元素时要注意的问题

    当使用母版,要使用js中的getElementById()方法取得某个内容页的元素时,所选取的id并不是母版中内容页的id,而是在设计内容页时设定的id例子:母版页: ...... <head ...

  5. 【LeetCode】133. Clone Graph (3 solutions)

    Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its nei ...

  6. 将form表单转化为json数据

    参考地址:https://github.com/hongymagic/jQuery.serializeObject

  7. EditPlus正则表达式中英文详解(附常用事例操作)

    http://www.cnblogs.com/JustinYoung/articles/editplus_regular_expression.html EditPlus正则表达式中英文详解 \t T ...

  8. C#搞工控的一些代码

    首先工控项目都会用到: using System.Runtime.InteropServices; 1.字节转化为单精度浮点数 2.单精度转成字节 3.使用结构体 4.使用动态链接库 5.ASCCII ...

  9. JS定义类及对象

    1.工厂方式 <script type="text/javascript"> function createObject(name){ var p = new Obje ...

  10. go 学习 ---package

    1.包简述 GO本身没有项目的概念,只有包,包括可执行包和不可执行包,而不管什么包,都应该包含在 $GOPATH/src 目录下,GO命令和编译器会在 $GOPATH/src 目录下搜索相应的包.比如 ...