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. 干货分享 9款精挑细选的HTML5应用

    对于前端开发者来说,分享一些优秀的HTML5应用可以直接拿来用,更重要的是可以激发创作的灵感.今天我们要分享9款精挑细选的HTML5应用,个个都是干货. 1.HTML5/CSS3滑块动画菜单 图标动画 ...

  2. opengl deferred shading

    原文地址:http://www.verydemo.com/demo_c284_i6147.html 一.Deferred shading技术简介 Deferred shading是这样一种技术:将光照 ...

  3. cocos2d - CCSprite各种动画

    转自 ITeye技术网站 // 触摸屏 -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { for( UITouc ...

  4. Hotspot JVM的常用选项

    Hotspot JVM的常用选项 选项的分类 Hotspot JVM提供以下三大类选项: 1. 标准选项:这类选项的功能是很稳定的,在后续版本中也不太会发生变化.运行java或者java -help可 ...

  5. asp.net gridview实现正在加载效果方案一AJAX(转)

    前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.as ...

  6. 基于HBase Hadoop 分布式集群环境下的MapReduce程序开发

    HBase分布式集群环境搭建成功后,连续4.5天实验客户端Map/Reduce程序开发,这方面的代码网上多得是,写个测试代码非常容易,可是真正运行起来可说是历经挫折.下面就是我最终调通并让程序在集群上 ...

  7. Windows 下 绿化 Oracle

    既然Oracle在非windows平台上可以很“绿色”的执行,那么在windows平台上有无可能呢? 答案是“Yes-No” 基本上,除了监听器(lisentner)这个异类外,Oracle实例完全可 ...

  8. Windows 7 完美安装 Visual C++ 6.0

    http://wenku.baidu.com/link?url=UiwoH2l4H_IWK6y8JkVNg4slp8gkM_9qudihP0XD4MdMCwm-j1-vINWEjQE1aBCeP121 ...

  9. 关于android 内存的笔记

    原文 https://developer.android.com/training/articles/memory.html 1.慎重使用Service,最好的办法是使用IntentService,一 ...

  10. Bootstrap 各种进度条详解

    一:默认的进度条 创建一个基本的进度条的步骤如下: 添加一个带有 class .progress 的 <div>. 接着,在上面的 <div> 内,添加一个带有 class . ...