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 ...
随机推荐
- 初学者的python学习笔记2——作业篇
这次的作业是商城展示,最开始的确是一脸懵逼,后来研究了一下网上的例子,总算是搞出来了一个(其实算是网上列子的简化版?). 最开始是想要使用字典去做,发现显示列表的时候怎么也无法实现序号和名称对应,原因 ...
- JQ对象到底是什么
jQuery对象是什么,举个例子,$('#id') 返回的就是jQuery对象,这个东西是整个jQuery的核心所在,所以我先来分析它. var jQuery = function( selector ...
- Python基础知识之排序法
在Python开发中,我们会经常使用到排序法,排序的最简单的方法是用sort(list)函数,它接受一个列表并返回与有序的元素一个新的列表. 原始列表不被改变. a = [5, 1, 4, 3] ...
- C#数组全解
数组概述 C# 数组从零开始建立索引,即数组索引从零开始.C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似.但还有一些差异应引起注意. 声明数组时,方括号 ([]) 必须跟在类型后面,而不 ...
- Android ScrollView与ListView的冲突解决办法汇总
1. public void setListViewHeight(){ ListAdapter listadapter = lv.getAdapter(); if (listadapter == n ...
- OJ-Triangle
这是Leet Code OJ上面的一道题,关于求从上到下的最小路径. 这是原题链接:https://leetcode.com/problems/triangle/ Given a triangle, ...
- 关于eclipse中DDMS中Emulator Control选项卡为灰色不可用
首先先感谢版主:http://blog.csdn.net/noname666/article/details/51670905#reply 方法一的出处:http://stackoverflow.co ...
- 常用类String的方法
String str="hello"; 1.与数组相关的方法 System.out.println(str.length()); System.out.println(str.ch ...
- SQL注入的分类
基于从服务器接收到的响应 基于错误的SQL注入 联合查询的类型 堆查询注射 SQL盲注 基于布尔SQL盲注 基于时间的SQL盲注 基于 ...
- 【Learning Python】【第四章】Python代码结构(一)
这一章的主旨在于介绍python的代码结构 缩进 在很多的编程语言中,一般{}用于控制代码块,比如以下的一段C代码 if(var <= 10) { printf("....." ...