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. 感谢CSDN赠送的图书和杂志(5月份)

    互联网的精神就是开放.就是分享.在分享的同一时候.我们也会收获到意外的回报. 近期.因为我在5月份发表了14篇博文,因此CSDN赠送了一本图书<软件系统架构>(本人自己选的)和一本< ...

  2. 保护HTTP的安全

    #如果没有严格的限制访问的权限,公司放在服务器上的重要文档就存在隐患,web需要有一些安全的http形式: #安全方法: #基本认证.摘要认证.报文完整性检查都是一些轻量级的方法,但还不够强大,下面介 ...

  3. yml在线格式转换工具(properties)

    分享一个在线properties 转 yml工具,也支持yml转properties, 域名非常简单好记,直接在地址栏里输入toyaml.com,地址:http://toyaml.com/ yml,即 ...

  4. 【laravel5.4】laravel5.4系列之生成_ide_helper.php文件

    在laravle中使用代码自动补全,比较方便开发,于是这边找到了相关的办法 在laravel配置完好的情况下,同时安装好了composer. 进入代码的根目录执行 composer require b ...

  5. Apache POI – Reading and Writing Excel file in Java

    来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...

  6. sudo: Cannot execute /usr/local/bin/zsh: No such file or directory 问题

    参考:sudo: Cannot execute /usr/local/bin/zsh: No such file or directory 之前在美化Ubuntu的时候,下了个zsh,但是忘记改配置文 ...

  7. Java Socket 编程之Socket与ServerSocket的区别

    http://www.cnblogs.com/hq-antoine/archive/2012/02/11/2346474.html 1.1 ServerSocket类    创建一个ServerSoc ...

  8. 那些恶心人的Screen基本概念

    Screen的这些基本概念中,最重要的就是dip的理解,而理解dip就是理解android适配不同设备的关键. Screen Size 实际物理尺寸.就是我们常说的3.5英寸屏幕,4.7英寸屏幕等等, ...

  9. Linux-支持中文

    转自:http://www.centoscn.com/CentosBug/osbug/2014/0919/3776.html 英文版的linux系统默认不支持中文显示 那么如何显示中文呢? 可以使用l ...

  10. python学习笔记011——内置函数sorted()

    1 描述 sorted() 函数对所有可迭代的对象进行排序操作. sorted() 与sort()函数之间的区别 1 排序对象 sorted:所有可迭代对象的排序 sort:list列表的排序 2 返 ...