LeetCode 39 Combination Sum(满足求和等于target的所有组合)
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的所有组合)的更多相关文章
- [LeetCode] 39. Combination Sum ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-39-Combination-Sum.html 描述 Given a set of candidate numbers (candidat ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
- [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 (C) (without duplicates) and a target number (T), find all unique c ...
- [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 --- java
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- [leetcode]39. Combination Sum组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
随机推荐
- iPhone 配置使用工具
“iPhone 配置实用工具”可让您轻松地创建.维护和安装配置描述文件及对配置描述文件进行加密,跟踪和安装预置描述文件与授权的应用程序,以及采集包括控制台日志在内的设备信息. http://suppo ...
- 用C++做微信公众平台开发的后台开发时,用sha1加密验证的方法
微信公众平台开发时,须要验证消息是否来自微信server,这要用到sha1加密算法.官网上给的是php的sha函数,C++中要用到以下这个函数: 一.引入头文件: #include<openss ...
- categorys
//spark-shell --driver-class-path /home/hadoop/test/mysqljdbc.jarimport java.sql.DriverManager// --q ...
- 184使用 Core Image 框架处理照片
实现图片的老旧.色彩.旋转效果 (1)使用 StoryBoard 故事版布局界面: (2)使用 Core Image 框架的 CIFilter 过滤器:分别对应的过滤器名称是:CISepiaTone( ...
- 升级到yosemite后homebrew报错的解决
报错会如下: /usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Ve ...
- [Unity3D]关于NaN(Not a Number)的问题
在游戏运行时,代码若写得不安全很容易出现NAN的异常.一旦NAN出现整个游戏不崩溃也坏死掉了,游戏上了则是要被直接打回来的节奏,更是一个开发及测试人员每人都要扣3000块的大BUG. 一般表现为: ...
- python获取数组中最多的元素
获取数组中数量最多的元素,也就是最频繁的那个元素,方法有很多,下面是3种最简单的: 用max函数 sample = [1,2,3,3,3,4,5,5] max(set(sample), key=sam ...
- 关于float样式
在认识float样式之前务必要认识position:absolute position:absolute 功能是:当前节点脱离文档流,对于其兄弟节点(即同一父节点下的节点)已经感觉不到他的存在(即他的 ...
- 8 -- 深入使用Spring -- 4...5 AOP代理:基于注解的“零配置”方式
8.4.5 基于注解的“零配置”方式 AspectJ允许使用注解定义切面.切入点和增强处理,而Spring框架则可识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5 一样的 ...
- 7 -- Spring的基本用法 -- 12... Spring 3.0 提供的表达式语言(SpEL)
7.12 Spring 3.0 提供的表达式语言(SpEL) Spring表达式语言(简称SpEL)是一种与JSP 2 的EL功能类似的表达式语言,它可以在运行时查询和操作对象图.支持方法调用和基本字 ...