leecode-39. Combination Sum
1、问题描述:
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]
] 2、边界条件:无
3、思路:先取一个数,然后与target比较;==则存贮,!=则继续从所有数里面选择。
从Level-N里面选择一个数,与目标比较,符合则存贮;不符合则再从所有数里面挨个取,从而化为同样的Level-N问题
形成递归。base case:1)与目标匹配;2)target - nums[i]<0,再减下去也是负数,这依赖于题目给的 全正数positive integers 条件
4、实现代码:
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> results = new ArrayList<>();
//Arrays.sort(candidates);
combinationSum(results, new ArrayList<Integer>(), candidates, target);
return results;
} public void combinationSum(List<List<Integer>> results, List<Integer> cur, int[] candidates, int target) {
if (0 == target) {
/**
Collections.sort(cur);//这里排序会把原列表改变,所以上层在恢复现场时出错。
if (!results.contains(cur)) {//去重
results.add(new ArrayList<Integer>(cur));
}
**/
ArrayList<Integer> result = new ArrayList<Integer>(cur);//先生成新的cur,然后进行排序
Collections.sort(result); //
if (!results.contains(result)) {
results.add(result);
return;
}
if (0 > target) {
return;
}
for (int i = 0; i < candidates.length; i++) {
cur.add(candidates[i]);
combinationSum(results, cur, candidates, target - candidates[i]);
cur.remove(cur.size() - 1);
}
}
}
5、时间复杂度:说不好; 空间复杂度:
6、题外知识:Arraylist排序:Collections静态排序API,Collections的排序都是稳定的。Collections.sort(List<T> list)、和Collections.sort(List<T> list,Comparator<?super T> c);使用的排序是稳定的,主要是对list排序。
链接:http://blog.csdn.net/tuke_tuke/article/details/51100219 和 http://www.importnew.com/17211.html
7、优化解法
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> results = new ArrayList<>();
Arrays.sort(candidates);//排序是为了使得答案有序;如果有重复数字的情况下,可以方便去重。
combinationSum(results, new ArrayList<Integer>(), candidates, target, 0);
return results;
} public void combinationSum(List<List<Integer>> results, List<Integer> cur, int[] candidates, int target, int start) {
if (0 == target) {
results.add(new ArrayList<Integer>(cur));
return;
}
if (0 > target) {
return;
}
for (int i = start; i < candidates.length; i++) { ///从start开始是因为前面的数字已经遍历过自己和后面的数字。
cur.add(candidates[i]);
combinationSum(results, cur, candidates, target - candidates[i], i);// not i + 1 because we can reuse same elements
cur.remove(cur.size() - 1);
}
}
}
最新文章
- [转载]iOS 10 UserNotifications 框架解析
- 启动tomcat时,报错:IOException while loading persisted sessions: java.io.EOFException解决方法
- 使用AStyle进行代码格式化
- HTTPS强制安全策略-HSTS协议阅读理解
- 教你把UltraEdit如何注册激活教程及UltraEdit 22.0.0.48 官方中文版下载
- HBase介绍及简易安装(转)
- EditText 密码属性
- 使用putty登陆cygwin出现server unexpectedly ...error.解决方案
- PowerShell 字符串操作符
- F - The Fun Number System(第二季水)
- theOS环境搭建
- C语言之for循环
- luoguP2502旅行
- temporal credit assignment in reinforcement learning 【强化学习 经典论文】
- 关于sql 索引
- django(八)之数据库表的一对多,多对多表-增删改查
- 【Spark2.0源码学习】-7.Driver与DriverRunner
- SuperMap 二维地图和三维场景弹窗窗口大小控制
- Test传送门(更新中)
- 深入理解Spring系列之六:bean初始化
热门文章
- 详解 pthread_detach()函数
- 查看linux连接进程占用的实时流量 -nethogs
- zjoi2015d1题解
- Codeforces Gym 101190 NEERC 16 .L	 List of Primes(递归)
- 【Python】数组排序
- 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)
- Flashback Database 闪回数据库
- C++ 创建文件的方法
- PowerShell 总结
- Zigbee协议栈--Z-Stack的使用