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. (个人)Linux基本指令收集

    1. 删除文件 其中 -r为向下递归删除    -f为强行删除,不做提示 rm -rf name 1   1 rm -rf name 2. 目录跳转指令 cd .. --跳转到上一级 cd ../ - ...

  2. MyBatis通用Mapper技巧

    一.排序 错误代码:example.orderBy(BaseEntity.Field.GMTUpdate + " desc"); 正确方式: 一是:通过注解 @OrderBy(va ...

  3. 几个免费IP地址查询API接口

    转:http://blog.csdn.net/ishxiao/article/details/52670242 -------------------------------------------- ...

  4. Oracle11g select查询时候输出未选定行

    解决方法是: 查询的表名是否是大写的: 是否没有提交执行结果:可以commit一下:

  5. asp.net单击头模板中的checkbox,实现datalist中所有chebox的全选和取消

    转载时请以超链接形式标明文章原始出处和作者信息及本声明http://blueseach.blogbus.com/logs/31281126.html 使用C#和javascript都可以实现,第二种更 ...

  6. Spring Boot项目使用Eclipse进行断点调试Debug

    1.在命令行下定位到项目根目录,启动Spring Boot项目,命令如下: java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=80 ...

  7. js压缩上传图片base64长度

    im发送图片,现将图片压缩再上传 1) 调用 FileReader 的 reader.readAsDataURL(img); 方法, 在其onload事件中, 将用户选择的图片读入 Image对象. ...

  8. python中的List 和 Tuple

    #-*- coding:UTF-8 -*- classmates=["Michael","Bob","Tracy"] print(class ...

  9. WebGL常用数学公式

    1.三角函数 坐标轴采用右手法则,沿Z轴的逆时针方向为正角度,假设原始点为p(x,y,z),a是X轴旋转到点p的角度,r是从原始点到p点的距离.用这两个变量计算出点p的坐标,等式如下: x = rco ...

  10. hibernate 之 sql查询

    如果用hibernate执行原生sql进行数据查询可以调用 SQLQuery query = getSession().createSQLQuery(sql); 然后再执行 query.list() ...