Problem: 给定数组并且给定一个target,求出所有满足求和等于target的数字组合
 
遍历所有的数组中元素,然后对target进行更新,将该元素添加到tempList中,直到remain等于0时达到条件,可以将该tempList添加到list中
 
注意:每个元素可以使用多次,因此每次的遍历都要从上次的那个下标开始。
 
当target更新到小于0的时候,返回,
当target更新到大于0的时候,进行从start下标开始遍历,并且将该数字添加到tempList中,递归调用。递归调用结束最后需要将tempList进行移除最顶的元素。
 
 
参考代码: 
package leetcode_50;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /***
*
* @author pengfei_zheng
* 求解满足加和等于target的所有数字组合
*/
public class Solution39 {
public List<List<Integer>> combinationSum(int[] nums, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, target, 0);
return list;
} private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){
if(remain < 0) return;
else if(remain == 0) list.add(new ArrayList<>(tempList));
else{
for(int i = start; i < nums.length; i++){
tempList.add(nums[i]);
backtrack(list, tempList, nums, remain - nums[i], i); // not i + 1 because we can reuse same elements
tempList.remove(tempList.size() - 1);
}
}
}
}

LeetCode 39 Combination Sum(满足求和等于target的所有组合)的更多相关文章

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

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

  2. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)

    题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数. ...

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

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

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

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

  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 组合之和

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

  7. Java [Leetcode 39]Combination Sum

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

  8. leetcode 39 Combination Sum --- java

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. [leetcode]39. Combination Sum组合之和

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

随机推荐

  1. 加密算法(对称加密)AES、DES (非对称加密)RSA、DSA

    目前主流的加密方式有:(对称加密)AES.DES        (非对称加密)RSA.DSA

  2. Linux 下 Nginx + JDK + Tomcat + MySQL 安装指南

    转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52026342 Nginx 是一款高性能的 http 服务器/反向代理服务器/电子邮 ...

  3. awk调用shell

    为什么会有这份记录:在帮同学传文件至服务器时,使用了scp,因此链接属性没有建立好,所以向通过awk完成.(更好的是通过tar传递) 附:awk中调用shell的方法. 参考:http://hi.ba ...

  4. swig和angular双花括号的冲突

    swig和angular都用{{name}}来作为模板中变量的取值, 那么要共用的话怎么办: {% raw %}{{ foobar }}{% endraw %} 或者 config(['$interp ...

  5. JUC回顾之-ConcurrentHashMap源码解读及原理理解

    ConcurrentHashMap结构图如下: ConcurrentHashMap实现类图如下: segment的结构图如下: package concurrentMy.juc_collections ...

  6. RancherOS(ROS)如何安装到硬盘? 并设置为用户自动登录到系统? -a rancher.autologin=tty1

    RancherOS 安装到硬盘,一般都是通过ssh_authorized_keys 方式. ------------------------------------------- 从第一次认识到这个方 ...

  7. Lua中调用函数使用点号和冒号的区别

    1.初学者最易混乱Top1——调用函数时用点号还是用冒号? 我们来看看下面的两句代码: mSprite.setPosition(, ); mSprite:setPosition(, ); 对于初次接触 ...

  8. Lua基础语法讲解

    Lua 是什么? Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. Lua 是巴西里约热内卢天主教大学( ...

  9. Synycovery 7.18f 一个优秀的同步软件

    Serial Key Name: Vdown RG Code: MCKOFA7MNGUQY7954

  10. SpringBoot------添加保存时自动编译插件

    .右键Java项目 .选择“Spring Tools” 3.选择“Add Boot DevTools” 4.每次使用Ctrl + S键时就会自动编译了 实际上是在Pom.xml文件中添加了如下Java ...