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. Java的代理模式(通过公共接口实现)

    代理模式的一种常见的实现方案是,定义一个接口或抽象类,并派生出目标子类,和代理子类.我们要操作的是目标子类里的方法,而很多时候,我们需要为目录子类中的方法增加额外的处理,如果增加日志功能.条件判断等, ...

  2. eclipse 如何运行mavenWeb项目

      1.使用maven 首先,在eclipse中,使用maven对项目进行打包: 其次,将项目发布到Tomcat服务器上 说明: demo_WebService2-0.0.1-SNAPSHOT文件夹存 ...

  3. 【Oracle】存储过程之完整篇

    1.语法 create [or replace] procedure pro_name[(parameter1,parameter2,...)] is|as begin plsql_sentences ...

  4. iOS 封装跑马灯和轮播效果

    代码地址如下:http://www.demodashi.com/demo/14075.html 功能概述和预览 功能描述:WSL_RollView 是基于UICollectionView实现的支持水平 ...

  5. 判断是否为SIM卡联系人

    判断是否为SIM卡联系人 在AsyncQueryContacts类中. private List<TxrjAccount> accounts = new ArrayList<Txrj ...

  6. 在win7/8下搭建简易的无线平台

    资料:http://www.cnblogs.com/KeenLeung/p/3482073.html http://www.cnblogs.com/KeenLeung/p/3481998.html 其 ...

  7. HDUOJ ------1398

    http://acm.hdu.edu.cn/showproblem.php?pid=1398 Square Coins Time Limit: 2000/1000 MS (Java/Others)   ...

  8. http请求No peer certificate的解决方法

    不少同学在做HTTP请求新浪授权或新浪数据的时候会出现 javax.net.ssl.SSLPeerUnverifiedException: No peer certificate的异常.现给出解决方法 ...

  9. Linux中断 - 驱动申请中断API

    一.前言 本文主要的议题是作为一个普通的驱动工程师,在撰写自己负责的驱动的时候,如何向Linux Kernel中的中断子系统注册中断处理函数?为了理解注册中断的接口,必须了解一些中断线程化(threa ...

  10. malloc的内存分配原理

    0 堆内存的在计算机内存中的形式 根据<The C Programming language>推测得到堆内存,图中的Heap区域即为堆内存块(Heap区域的数目不代表计算机堆内存的真实数目 ...