【leetcode刷题笔记】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]
题解:类似八皇后问题。每次把candidates[i]加入到numbers列表中,然后递归的搜索target-candidates[i]的组成方法。
要注意的一点是,将candidates[i]加入到numbers列表后,i前面的元素就不能再往numbers里面加了,只能再加入i本身或者i后面的元素。所以在递归函数中要有一个参数start,表明只有start本身或者以后的元素可以加入numbers中。每当递归函数参数target等于0的时候,说明找到了一组答案在numbers中,把numbers加入到answer里面就可以了。
代码如下:
public class Solution {
private void combiDfs(int[] candidates,int target,List<List<Integer>> answer,List<Integer> numbers,int start){
if(target == 0){
answer.add(new ArrayList<Integer>(numbers));
return;
}
for(int i = start;i < candidates.length;i++){
if(candidates[i] > target)
break;
numbers.add(candidates[i]);
combiDfs(candidates, target-candidates[i], answer, numbers,i);
numbers.remove(numbers.size()-1);
}
}
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> answer = new ArrayList<List<Integer>>();
List<Integer> numbers = new ArrayList<Integer>();
Arrays.sort(candidates);
combiDfs(candidates, target, answer, numbers,0); return answer; }
}
【leetcode刷题笔记】Combination Sum的更多相关文章
- 【leetcode刷题笔记】Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- (python)leetcode刷题笔记 01 TWO SUM
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【leetcode刷题笔记】Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- Ajax系列之中的一个:ajax旧貌换新颜
什么是ajax? 什么是Ajax? Ajax就是Asynchronous +JavaScript+XML.中文翻译为:异步的javascript与XML,它是利用javascript语言和xml数据实 ...
- 【海盗派测试分析MFQ&PPDCS】读书笔记
使用脑图花了一张读书笔记,可能有点长
- java 安装后 不能 java javac 说找不到命令 -bash: javac: command not found
java 安装后 不能 java javac 说找不到命令 -bash: javac: command not found 不是环境变量的问题, 直接cd到java的目录 也不能执行命令 后来发现是 ...
- thinkphp5.0极速搭建restful风格接口层实例
作为国内最流行的php框架thinkphp,很快就会发布v5.0正式版了,现在还是rc4版本,但已经很强大了下面是基于ThinkPHP V5.0 RC4框架,以restful风格完成的新闻查询(get ...
- eventfd
#include <sys/eventfd.h> int eventfd(unsigned int initval, int flags); eventfd() creates an &q ...
- kafka 小案例【一】---设置但个消息集群
启动kafka服务 [ bin/kafka-server-start.sh config/server.properties ] [root@zhangxs kafka_2.]# bin/kafka- ...
- 隐藏VS2013的反馈、通知和登录按钮
Visual Studio 2013的右上角有反馈.通知.登录.快速启动等按钮,在VS2013中没有选项可以设置为隐藏. 打开注册表(开始 -> 运行 -> regedit),展开到以下路 ...
- CocoaPods报错:The dependency `` is not used in any concrete target
内容提要: podfile升级之后到最新版本,pod里的内容必须明确指出所用第三方库的target,否则会出现The dependency `` is not used in any concrete ...
- iOS 生命周期 -init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 区别和用途
iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...
- C语言基础知识【C语言教程】
2017年7月7日23:15:51外边下雨,突然想学习c语言,所以刷一遍基础. 笔记:C 语言教程1.C 语言是一种通用的.面向过程式的计算机程序设计语言.1972 年,为了移植与开发 UNIX 操作 ...