039 Combination Sum 组合总和
给一组候选数字(C)(没有重复)并给一个目标数字(T),找出 C 中所有唯一的组合使得它们的和为 T。
可以从 C 无限次数中选择相同的数字。
说明:
所有数字(包括目标)都是正整数。
解集合中没有相同组合。
例如,给一个候选集合 [2, 3, 6, 7] 并且目标为 7,
一个解的集合为:
[
[7],
[2, 2, 3]
]
详见:https://leetcode.com/problems/combination-sum/description/
Java实现:
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<Integer> out=new ArrayList<Integer>();
Arrays.sort(candidates);
helper(candidates,target,0,out,res);
return res;
}
private void helper(int[] candidates, int target,int start,List<Integer> out,List<List<Integer>> res){
if(target<0){
return;
}
if(target==0){
res.add(new ArrayList<Integer>(out));
}else{
for(int i=start;i<candidates.length;++i){
out.add(candidates[i]);
helper(candidates,target-candidates[i],i,out,res);
out.remove(out.size()-1);
}
}
}
}
参考:http://www.cnblogs.com/grandyang/p/4419259.html
039 Combination Sum 组合总和的更多相关文章
- 【LeetCode】Combination Sum(组合总和)
这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...
- Leetcode39.Combination Sum组合总和
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- LeetCode 039 Combination Sum
题目要求:Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique c ...
- Java for LeetCode 039 Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [leetcode]39. Combination Sum组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- LeetCode OJ:Combination Sum (组合之和)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode】039. Combination Sum
题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...
随机推荐
- openfire插件(1)
插件核心类,这里对PacketInterceptor.Plugin进行继承.如果开发插件就一定要继承Plugin,而继承PacketInterceptor是拦截用户发送的消息包.对消息包进行过滤.拦截 ...
- 【Shell】变量的取用、删除、取代与替换
——来自<鸟哥的Linux私房菜> ——总结做方便查阅之用 变量的取用: echo echo $variableecho $PATHecho ${PATH} 变量的配置守则1.变量与变量内 ...
- xxx referenced from: xxx in xxx.o
情形一:可能是有一些源码文件没有加入工程所导致的,找到相应的.h和.m文件,将其add进入项目工程即可解决这种问题. 情形二:也有可能是某些framework没有加入项目中, 示例: Undefi ...
- AtCoder Grand Contest #026 A - Colorful Slimes 2
Time Limit: 2 sec / Memory Limit: 1024 MB Score : 200200 points Problem Statement Takahashi lives in ...
- UI 界面:技术决定一切
转自:http://www.cnblogs.com/NEOCSL/archive/2012/12/10/2811153.html 在我看来,肖恩帕克不仅仅是一位技术天才和远见卓识的移动互联网领域先锋. ...
- MVC 模式介绍(1)
MVC 模式 MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式.这种模式用于应用程序的分层开发. Model(模型) - 模型代表一个存取数据的对象或 JAVA ...
- NancyFX 第一章 NancyFX 简介
Nancy是.NET 平台的微框架.在受到Ruby社区的Sinatra框架启发下,NancyFx框架提供一个.NET平台下的低门槛.易上手的可用于Web开发工具包. 请注意我说的是可用于Web开发,这 ...
- <正则吃饺子> :关于oracle 中 exists 、not exists 的简单使用
话不多说,简单的总结而已.网络上很多很详细介绍. 例如,博文:http://blog.csdn.net/zhiweianran/article/details/7868894 当然这篇也是转载的,原 ...
- JavaScript与DOM常见面试题
1. JavaScript 1.1.简要描述 JavaScript的数据类型? 参考答案: Java Sc ri pt 的数据类型可以分为原始类型和对象类型.原始类型包括 string. number ...
- sass安装方法,绝对好用的方式
系统重做了,很多东西都重装,sass也一样,结果在安装的过程中遇到了问题,这里记录下,也给同样遇到问题的朋友们一个思路.本方法是参照http://www.w3cplus.com/sassguide/i ...