LeetCode OJ 39. Combination Sum
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]
Subscribe to see which companies asked this question
【题目解析】
给定一个候选数字序列一个目标值,找到所有的和等于目标值的候选数字组合。同一个候选数字可以出现多次。
1. 所有的数字是正数;
2. 组合中的数字降序排列;
3. 结果中不能存在相同的组合;
【思路】
首先我们从一个简单的例子分析一下这样组合生成的过程:
[1,2,3] target = 7
我们从[1,2,3]生成所有的和等于7的数字组合,可以分为以1开头的有:
[1,1,1,1,1,1,1],[1,1,1,1,1,2],[1,1,1,1,3],[1,1,1,2,2],[1,1,2,3],[1,2,2,2],[1,3,3]
以2开头的:
[2,2,3]
上面这几个组合是满足所有条件的组合。生成过程可以这样描述:
1. 给定一个升序排序的数组;
2. 从数组头部向后遍历,如果遇到一个比目标值小的数n,我们找到目标值为target - n的所有组合,如果找到这样的组合,那么我们把n合并到每一个组合里;
3. 如果遇到一个值m = target 则新建一个List,添加m后返回;
4. 如果遇到一个值m > target 则终结遍历,因为之后的数字肯定比target还大;
很明显这是一个递归的过程,在这个过程中我们首先对候选数组进行了排序,这是为什么呢? 因为在递归的过程中,如果一个数字n小于target,那么在递归求解target - n时我们可以确定一个从候选数组重新开始的下标,这个下标就是当前数字的下标。
【java代码】
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates); //升序排序
return combination(candidates, target, 0);
}
public List<List<Integer>> combination(int[] candidates, int target, int start) {
List<List<Integer>> list = new ArrayList<>();
if(candidates == null || candidates.length == 0) return list;
for(int i = start; i < candidates.length; i++){
if(candidates[i] < target){
List<List<Integer>> tlist = combination(candidates, target - candidates[i], i);
if(tlist.size() > 0){
for(List<Integer> alist : tlist){
alist.add(0, candidates[i]);
}
list.addAll(tlist);
}
}
else if(candidates[i] == target){
List<Integer> tlist = new LinkedList<>();
tlist.add(target);
list.add(tlist);
}
else break;
}
return list;
}
}
LeetCode OJ 39. Combination Sum的更多相关文章
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- LeetCode OJ 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode题解39.Combination Sum
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【一天一道LeetCode】#39. Combination Sum
一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode OJ:Combination Sum (组合之和)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode OJ:Combination Sum III(组合之和III)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- Arch安装KDE5
plasma desktop Install the plasma-meta meta-package or the plasma group. Alternatively, for a more m ...
- 在tableview的headerView中添加webView,webView自适应高度
最近在项目中需要添加一个webView加载的页面,下面显示的是对这个webView所显示的内容的一个评论列表 ,列表要根据后台加载过来的HTML自适应的变化高度,tableview的cell在webV ...
- 一个简单的例子说明windows环境变量配置
关于win下环境变量的问题 配置环境变量其实就像是创建一个快捷键一样,我们把安装程序的路径告诉系统环境变量,这样下次我们在命令行中就可以直接使用一个简单的命令来调用我们安装的程序,因为此时计算机已经知 ...
- CSU 1810 Reverse
湖南省第十二届大学生计算机程序设计竞赛$H$题 规律,递推. 这种问题一看就有规律.可以按位统计对答案的贡献.即第$1$位对答案作出了多少贡献,第$2$位对答案作出了多少贡献.....累加和就是答案. ...
- unity笔录
----------------------------unity项目在启动splash的时候黑屏 原因不明------------------测试复制项目 用剔除法测试 笔录开始 先用原版本打包 ...
- php的redis的pconnect
1. 当使用pconnect时,连接会被重用,连接的生命周期是fpm进程的生命周期,而非一次php的执行. 2.如果代码中使用pconnect, close的作用仅是使当前php不能再进行redis请 ...
- iOS学习之Runtime(一)
一.Runtime简介 因为Objective-C是一门动态语言,所以它总是想办法把一些决定性工作从编译链接推迟到运行时,也就是说只有编译器是不够的,还需要一个运行时系统(runtime system ...
- CSS预处理器 Less Sass,Scss 编译 Sourcemap调试
sass.less和stylus的安装使用和入门实践 SASS用法指南 Sass Basics CSS预处理器 css preprocessor 预处理器即preprocessor,预处 ...
- Python基础篇-day7
本节目录-面向对象1 类介绍1.1 面向对象oo特征1.2 类的特性1.3 创建与调用 1.3.1 基本结构 1.3.2 结构说明 1.3.3 对外部提供只读访问接口 1.3.4 析构方法2 继承2. ...
- 折腾一天,终于配置好了,ssl证书,启用了https,用的阿里云ECS服务器
阿里云ECS服务器配置了ssl证书, httpd-ssl.conf 的配置很重要,网站目录一定要设置正确. 阿里云的虚拟空间,弹性Web,目前好像还不支持ssl证书. 最后要网站强制https,下面 ...