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的更多相关文章

  1. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  2. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  3. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  4. Java [Leetcode 39]Combination Sum

    题目描述: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in  ...

  5. LeetCode 39. Combination Sum (组合的和)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  6. LeetCode 39 Combination Sum(满足求和等于target的所有组合)

    题目链接: https://leetcode.com/problems/combination-sum/?tab=Description   Problem: 给定数组并且给定一个target,求出所 ...

  7. [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)

    https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...

  8. 39. Combination Sum (Java)

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  9. Leetcode 39. Combination Sum

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

随机推荐

  1. JSP初识

    JSP最终会变成一个完整的servlet在web应用中运行.它与其他的servlet非常相似,只不过这个servlet类会由容器编写. 1.JSP的生命周期 如果一个web应用包含JSP,部署这个应用 ...

  2. android asyncTask 详解

    只看http://www.cnblogs.com/xiaoluo501395377/p/3430542.html  足以

  3. matlab函数调用及数据传递

    http://www.cnblogs.com/duanp/archive/2008/11/29/Matlab-GUIDE.html函数调用 在一个m文件中,可以定义多个函数,但是文件名一定要与第一个函 ...

  4. 记录一些容易忘记的属性 -- UIGestureRecognize手势

    //一个手势只能添加到一个view上面 //设置当前手势需要的点击次数    _tapRec.numberOfTapsRequired = 1;//(默认为1)    //设置当前需要几个手指同时点击 ...

  5. hdoj-2019

    #include "stdio.h"void sort(int number[],int n,int m);int main(){ int n,m,i,number[100]; s ...

  6. (spring-第5回【IoC基础篇】)spring容器从加载配置文件到实例化bean的内部工作机制

    前面讲过,spring的生命周期为:实例化前奏-->实例化-->实例化后期-->初始化前期-->初始化-->初始化后期-->bean的具体调用-->销毁前-- ...

  7. BZOJ 1690 奶牛的旅行

    分数规划. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...

  8. Python行和缩进

    Python缩进和冒号 对于Python而言代码缩进是一种语法,Python没有像其他语言一样采用{}或者begin...end分隔代码块,而是采用代码缩进和冒号来区分代码之间的层次. 缩进的空白数量 ...

  9. Mac可设置环境变量的位置、查看和添加PATH环境变量

    Mac 启动加载文件位置(可设置环境变量) 首先要知道你使用的 Mac OS X 是什么样的 Shell,使用命令 echo $SHELL 如果输出的是:csh 或者是 tcsh,那么你用的就是 C ...

  10. 查单神器v1.0 升级 →B站看鬼畜神器v1.0

    去年学校实习,我去了一家快递输单公司工作.工作任务就是把运单图片上的内容(寄件人,地址之类)输入到公司的数据库里.每天输单结束后,还要对一些容易错的运单进行排查.单量多的时候一天甚至要查千张以上的运单 ...