LeetCode--Combination Sum --ZZ
http://blog.csdn.net/linhuanmars/article/details/20828631
这个题是一个NP问题,方法仍然是N-Queens中介绍的套路。基本思路是先排好序,然后每次递归中把剩下的元素一一加到结果集合中,并且把目标减去加入的元素,然后把剩下元素(包括当前加入的元素)放到下一层递归中解决子问题。算法复杂度因为是NP问题,所以自然是指数量级的。Java代码如下:
public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(candidates == null || candidates.length==0)
return res;
Arrays.sort(candidates);
helper(candidates,0,target,new ArrayList<Integer>(),res);
return res;
}
private void helper(int[] candidates, int start, int target, ArrayList<Integer> item,
ArrayList<ArrayList<Integer>> res)
{
if(target<0)
return;
if(target==0)
{
res.add(new ArrayList<Integer>(item));
return;
}
for(int i=start;i<candidates.length;i++)
{
if(i>0 && candidates[i]==candidates[i-1])
continue;
item.add(candidates[i]);
helper(candidates,i,target-candidates[i],item,res);
item.remove(item.size()-1);
}
}
注意在实现中for循环中第一步有一个判断,那个是为了去除重复元素产生重复结果的影响,因为在这里每个数可以重复使用,所以重复的元素也就没有作用了,所以应该跳过那层递归。这道题有一个非常类似的题目Combination Sum II,有兴趣的朋友可以看看,一次搞定两个题哈。
LeetCode--Combination Sum --ZZ的更多相关文章
- [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 ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- [Leetcode] Combination Sum 系列
Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
随机推荐
- URL中参数为数组
今天写代码时候碰到了一个需要在URL中传递数组类型的参数,记录一下. var urlstr = "http://test"; var test = new Array(); for ...
- ifconfig command not found
CentOS minimal 命令做了修改 可以运行 ip addr
- 移远EC20的使用
一 发短信 3. 推荐短信流程3.1 查询 短信存储区AT+CPMS?+CPMS: "ME",19,255,"ME",19,255,"ME" ...
- android LinearLayoutForListView
由于 scrollview 套 listview 会有很多问题,网上很多人用 LinearLayout 模拟 listview, 也可以设置 adapter. 很多人直接继承 BaseAdapter, ...
- 案例17-validate自定义校验规则校验验证码是否输入正确
1 自定义校验规则代码 <script type="text/javascript"> //使用validate插件进行表单的校验 $(function(){ $(&q ...
- Java 实例 - 标签(Label)
Java 实例 Java 中的标签是为循环设计的,是为了在多重循环中方便的使用break 和coutinue . 以下实例当在循环中使用 break 或 continue 循环时跳到指定的标签处: ...
- web.xml配置文件中async-supported报错解决
项目中配置spring时async-supported报错: 是因为<async-supported>true</async-supported>是web.xml 3.0的新特 ...
- golang学习之win7下go环境搭建
以下均采用windows64环境,首先是go的下载,go有msi安装安装和zip解压安装两种安装方式,使用msi安装后go环境会自动配置,zip解压后需手动配置各种环境变量. 首先是下载,网上一搜一大 ...
- mac os下载安装jmeter
一.简介 jmeter是属于apache的一个开源产品,纯Java应用.最初用来进行功能测试,而后又扩展了更多的测试功能. 二.下载 进入apache的jmeter下载页:http://jmeter. ...
- Spring课程 Spring入门篇 5-6 introductions应用
1 解析 1.1 aop:declare-parents 标签简介 1.2 标签使用样式 2 代码演练 2.1 introductions标签应用 1 解析 1.1 aop:declare-paren ...