leetcode 39 Combination Sum --- java
Given a set of candidate numbers (C) 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 combinationSum {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
getResult(candidates,target,0,result,new ArrayList<Integer>());
return result;
}
public void getResult( int[] candidates, int target,int pos, List<List<Integer>> result,List<Integer> ans){
for( int i = pos;i <candidates.length; i++){
if( target == candidates[i]){
ans.add(candidates[i]);
result.add(new ArrayList<Integer>(ans));
ans.remove(ans.size()-1);
return;
}
else if(target > candidates[i]){
ans.add(candidates[i]);
getResult(candidates,target-candidates[i],i,result,ans);
ans.remove(ans.size()-1);
}else
return ;
}
}
/*
* 1.给出一个数组以及一个目标数,求出用数组中的数相加等于目标数的所有结果(数组中的数可以重复);
* 2.78+21
*/
}
主要是用递归的方法,如果小与target那么接着加,直到等于target或者大于target为止。有点类似于八皇后。
例如,给定{2,2,3}和7 由于可以重复数字,(其实相当于{2,3}和{7}),先进行排序
那么 2<7 --------> (2+2)<7 --------> (2+2+2)<7 --------> (2+2+2+2)>7 舍去 ,由于+2已经大于7,那么剩下的也都会大于7. 之后的+3就可以舍去了
--------> (2+2+2+3)>7
--------> (2+2+3)=7 获得一个答案,之后的数字也不用计算,因为肯定会比7要大
--------> (2+3)<7 --------> (2+3+3)>7 舍去
3<7 --------> (3+3)<7 --------> (3+3+3)>7 舍去
这就得到了所有的答案。
但是结果并不是特别理想,然后做下列调整:
1.尽量减少new ArrayList<Integer>()的操作,新建对象的操作会增加运行时间和内存。
2.也可以使用DP,但是就结果而言,还是递归比较好。
3.最后发现,如果不用List<Integer>而改用数组,那么就会击败所有用户,达到最快
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
getResult(candidates,target,0,result,new int[target],0);
return result;
}
public void getResult( int[] candidates, int target,int pos, List<List<Integer>> result,int[] ans,int num){
for( int i = pos;i <candidates.length; i++){
if( target == candidates[i]){
List<Integer> aa = new ArrayList<Integer>();
for( int ii =0; ii<num; ii++)
aa.add(ans[ii]);
aa.add(candidates[i]);
result.add(aa);
return;
}
else if(target > candidates[i]){
ans[num] = candidates[i];
getResult(candidates,target-candidates[i],i,result,ans,num+1);
}else
return ;
}
}
}
leetcode 39 Combination Sum --- java的更多相关文章
- [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 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- Java [Leetcode 39]Combination Sum
题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode 39. Combination Sum (组合的和)
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所 ...
- [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...
- 39. Combination Sum (Java)
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- Leetcode 39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
随机推荐
- Android 查看webview里面的图片
今天介绍一下怎么查看WebView里面的图片,首先要设置WebView能够支持JavaScript,然后实现JavaScript的监听接口: mWebView.getSettings().setJav ...
- 如何利用SVN合并代码
一. 背景 平时在进行开发时,一般都会有多版本同时进行,包括项目版本.周版本.紧急版本等,当某一个版本具备上线条件后,需要在上一个已发布的版本基础上进行发布,才能够避免出现版本相互覆盖,因此 ...
- C#中的五个访问修饰符
一.public, private, protected, internal, protected internal 1.public : 公开的, 公共的 2.private : 私有的 (只能在当 ...
- Iterator之java.util.ConcurrentModificationException
在运行以下代码时,会报java.util.ConcurrentModificationException异常, public class Demo { public static void main( ...
- 0911 Socket网络编程
1.实现ftp上传.下载功能 1.1 循环接收数据直到接收完毕 server端接收client发送的命令(比如说ifconfig),然后server端将命令执行结果反馈给客户端,这时候有个问题,ser ...
- php大力力 [015节]兄弟连高洛峰php教程(土豆网栏目地址)
兄弟连高洛峰php教程 兄弟连高洛峰php教程(土豆网栏目地址) [2014]兄弟连高洛峰 PHP教程1.1.1 新版视频形式介绍 [2014]兄弟连高洛峰 PHP教程1.1.2 BS结构软件类型介绍 ...
- River Crossing 简单的动态规划 .
第一行 t 表示有几组测试数据 . 每组测试数据的 第一行是 n, m . 然后 下面有n行数据 . 题意:有1个人和N只羊要过河.一个人单独过河花费的时间是M,每次带一只羊过河花费时 ...
- bootstrap菜单完美解决---原创
由于bootstrap的各方优点,偶的“点金项目细化分包管理平台”决定采用它.但在使用中遇到了一些问题,比如菜单的问题,这个菜单是用的一个JQuery的一个效果,点击后,所点击的链接处的class要加 ...
- IOS 作业项目(4)步步完成 画图 程序(问题处理)终结
一,解决换色程序崩溃问题 程序崩溃是因为颜色的内存被释放,添加如下类的内容即可 @implementation TuyaPath - (id)init { self = [super init]; i ...
- Candy Distribution_找规律
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6012 Accepted: 3341 Description N chi ...