Combination Sum
Given a set of candidate numbers (C) (without duplicates) 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.
- 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]
]
解题思路:深度遍历,找出所有合格的数据。
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
Arrays.sort(candidates);
work(target,0,candidates,res,new ArrayList<Integer>());
return res;
}
//胜读遍历,找出合格的数据
/*
* target:代表目标数据
* index:代表循环的其实位置,也就是查找合格数据的额起始位置
* candidates 候选值数组
* res:返回值
* arrayList:保存一组合格数据的变量
* */
public void work(int target, int index, int[] candidates, List<List<Integer>> res, ArrayList<Integer> arrayList){
//for循环,每次从index出发,因为数组已经排序,所以不会出现重复的数据
//终止条件为索引越界&&目标值要大于等于当前要检查的候选值
for(int i=index;i<candidates.length&&candidates[i]<=target;i++){
/*
* 如果target大于当前从candidate中提取的值时,则可以将其加入到arrayList中,在进入深度的遍历查找合格数据
* 注意的是,当无论是查找成功还是失败的时候,都要将arrayList的最后一个数据弹出,一遍进行下一次的深度遍历
* */
if(candidates[i]<target){
arrayList.add(candidates[i]);
work(target-candidates[i], i, candidates, res, arrayList);
arrayList.remove(arrayList.size()-1);
}
/*
* 如果target==当前提取的candidate中的值,则表明查找成功,将这一数组添加到res横中
* 并且弹出弹出arrayList中的最后一个数据进行下一次的遍历
* */
else if(candidates[i]==target){
arrayList.add(candidates[i]);
res.add(new ArrayList<Integer>(arrayList));
arrayList.remove(arrayList.size()-1);
}
}
}
}
Combination Sum的更多相关文章
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- 【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
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
随机推荐
- c++中变量声明和变量定义的区别。2016年12月6日
整个流程: 1.程序告诉cpu,程序将要使用一个变量.(暂时不一定用到,先说一下.) 2.程序告诉CPU,程序现在就要使用一个变量.(现在就用) 3.cpu按照这个变量的类型,把内存划分出几个单位(b ...
- 使用WebDriverWait类处理等待(sleep)的问题
用selenium进行web UI的自动化开发时,经常遇到loading需要等待的时候,或者需要验证一个action之后某个dialog是否呈现或者消失.对于这类情况是不建议用sleep(xx)来死等 ...
- 初学Scala
由于项目的需要接触了Scala语言,也花了一些时间来了解和学习Scala,今天对学习的Scala进行一个小的总结. 1.What is Scala? Scala的官方网站上给出的解释是: Scala ...
- JQ对象到底是什么
jQuery对象是什么,举个例子,$('#id') 返回的就是jQuery对象,这个东西是整个jQuery的核心所在,所以我先来分析它. var jQuery = function( selector ...
- php项目中使用memcache
memcache为分布式内存缓存系统,通过在内存中存取数据和对象减少对数据库的访问从而减轻数据库的负载,memcache分为客户端和服务端,服务端为C实现,有现成的服务端应用程序,无需开发,只需要配置 ...
- iOS交互WebService(cxf框架)
公司后台java用的cxf框架,说是iOS.Android.web客户端都可以通用,但是我还是第一次遇到,所以做的时候遇到了不小的坑.下面总结下我开发中遇到的问题以及解决方案: 首先,后台提供了一份接 ...
- Java并发编程学习笔记(三)——对象的组合
重要概念: 1.在设计线程安全类的过程中,需要包含以下三个基本要素: (1)找出构成对象状态的所有变量. (2)找出约束状态变量的不变性条件. (3)建立对象状态的并发访问管理策略. 2.
- RABBITMQ(小总结 持续更新...
(一)理解消息通信 1.消息通信概念---消费者.生产者和代理 生产者(producer)创建消息,然后发送到代理服务器(RaabitMQ). 其中消息包括两部分内容:有效载荷(payload)和标签 ...
- DotNetBar中TextBoxDropDown效果图
- pip和requests模块的安装
1.配置python的环境变量 在path中加入pyhton的环境变量,如我的是E:\Python27 2. 如果没有pip,一个帮助软件管理的东东(现在的版本一般有,若有跳到3) 去https:// ...