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);
}
}
}
leecode-39. Combination Sum的更多相关文章
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode题解39.Combination Sum
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode笔记:39. Combination Sum
题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...
- 39. Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
随机推荐
- mongdb启动问题
问题:Detected unclean shutdown - /data/db/mongod.lock is not empty. old lock file:/data/db/mongod.lock ...
- ogg概叙、架构、进程
一. OGG 概述 OGG 全称Oracle Golden Gate. 历史: Golden Gate公司于1995年成立于美国加州旧金山,它的名称源自旧金山闻名于世的金门大桥.两位创始人Eric F ...
- bzoj 4453 cys就是要拿英魂! —— 后缀数组+单调栈+set
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4453 这种问题...一般先把询问离线,排序: 区间对后缀排名的影响在于一些排名大而位置靠后的 ...
- javascript中原型链存在的问题
我们知道使用原型链实现继承是一个goodway:)看个原型链继承的例子. function A () { this.abc = 44; } A.prototype.getAbc = function ...
- DCloud-HTML5+:5+ App开发入门指南
ylbtech-DCloud-HTML5+:5+ App开发入门指南 1.返回顶部 1. 5+ App开发入门指南 App App入门 HTML5 Plus应用概述 HTML5 Plus移动App,简 ...
- openstack开发环境搭建
1 目的 让linux下的openstack代码能在windows上面实现同步开发. 2 目标 使用samba实现window与Linux的文件共享. 3 实验环境 ...
- python斐波拉契数列
def fib(max): n, a, b = 0, 0, 1 while n < max: print(b) a, b = b, a + b n = n + 1 return 'done' 注 ...
- iView之DatePicker的datetimerange校验
使用DatePicker的type是datetimerange时,处理开始--结束的持续时间校验如下.遇到的问题:时间弹出校验提示,但是程序还是会继续往下走,所以调完校验后,再做判断开始时间是否为tr ...
- 快速部署Kubernetes集群管理
这篇文章介绍了如何快速部署一套Kubernetes集群,下面就快速开始吧! 准备工作 //关闭防火墙 systemctl stop firewalld.service systemctl disabl ...
- Laravel框架之CSRF防跨站攻击
laravel框架的csrf防跨站攻击,简单的意思就是说为了防止别人自己写表单非法提交,非法绕过前台的验证,直接将数据往后台执行. 一般的网站如果没有这些安全措施,比较容易被攻击.当然了也还要有其他的 ...