LeetCode: Combination Sum 解题报告
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 解题报告
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 解题报告的更多相关文章
- 【LeetCode】39. Combination Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...
- 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 ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [Leetcode] Combination Sum 系列
Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- eclipse 将javaWeb项目转化成maven项目
eclipse 将javaWeb项目转化成maven项目 CreateTime--2018年4月18日16:04:18 Author:Marydon 1.首先,maven项目的标准目录 2.web ...
- Android Studio关于USB device not found的解决的方法
Android Studio关于USB device not found的解决的方法 我们使用Android Studio进行Android开发时.当我们使用真机进行调试时.非常可能会出现USB de ...
- 在Ubuntu16.04 64bit上安装sublime text 3
安装sublime text 3 根据官网上提供的安装说明 https://www.sublimetext.com/docs/3/linux_repositories.html 进行安装, 首先是 ...
- HashTable、List、ArrayList的经典使用和相互转换
1.添加引用 using System.Collections; 2.创建并添加数据 Hashtable hs = new Hashtable(); hs.Add("Name1", ...
- oracle客户端服务端字符集-解决乱码
查询server段字符集 select userenv('language') from dual 查询client段字符集 select * from v$nls_parameters NLS_LA ...
- HDUOJ--畅通工程
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- C#搞工控的一些代码
首先工控项目都会用到: using System.Runtime.InteropServices; 1.字节转化为单精度浮点数 2.单精度转成字节 3.使用结构体 4.使用动态链接库 5.ASCCII ...
- Unix环境高级编程(十七)网络IPC套接字
通过网络套接字可以使得不同计算机上运行的进程相互通信. 1.创建套接字 #include <sys/socket.h> Int socket( int domain, int type, ...
- 选择问题 and 字谜游戏问题
#include <stdio.h> #include <stdlib.h> // 第一题 // 找出N个数的第k个最大者 // 方法1:排序(冒泡),降序找出第k个值 // ...
- linux page buffer cache深入理解
Linux上free命令的输出. 下面是free的运行结果,一共有4行.为了方便说明,我加上了列号.这样可以把free的输出看成一个二维数组FO(Free Output).例如: FO[2][1] = ...