[leetcode]39. Combination Sum组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
- All numbers (including
target) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
Input: candidates =[2,3,6,7],target =7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5],target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
题意:
给定一个集合以及一个值target,找出所有加起来等于target的组合。(每个元素可以用无数次)
Solution1: Backtracking
code:
/*
Time: O(n!) factorial, n!=1×2×3×…×n
Space: O(n) coz n levels in stack for recrusion
*/ class Solution {
public List<List<Integer>> combinationSum(int[] nums, int target) {
Arrays.sort(nums); // 呼应dfs的剪枝动作
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
dfs(nums, path, result, target, 0);
return result;
} private static void dfs(int[] nums, List<Integer> path,
List<List<Integer>> result, int remain, int start) {
// base case
if (remain == 0) {
result.add(new ArrayList<Integer>(path));
return;
} for (int i = start; i < nums.length; i++) {
if (remain < nums[i]) return; //基于 Arrays.sort(nums);
path.add(nums[i]);
dfs(nums, path, result, remain - nums[i], i);
path.remove(path.size() - 1);
}
}
}
[leetcode]39. Combination Sum组合之和的更多相关文章
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- [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] Combination Sum 组合之和
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 (C) (without duplicates) and a target number (T), find all unique c ...
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所 ...
- LeetCode OJ:Combination Sum (组合之和)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode】Combination Sum(组合总和)
这道题是LeetCode里的第39道题. 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组 ...
- leetcode 39 Combination Sum --- java
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
随机推荐
- Java 中int、String的类型转换
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- 什么是pytorch(3神经网络)(翻译)
神经网络 torch.nn 包可以用来构建神经网络. 前面介绍了 autograd包, nn 依赖于 autograd 用于定义和求导模型. nn.Module 包括layers(神经网络层), 以及 ...
- Javascript 香蕉分段吃(数组分隔)
Javascript 香蕉分段吃(数组分隔) function chunk(arr, size) { var newArr =[]; for(var i = 0; i < arr.length; ...
- Hystrix 学习使用
说明: 每次调用创建一个新的HystrixCommand,把依赖调用封装在run()方法中 执行execute()/queue做同步或异步调用 请求接收后,会先看是否存在缓存数据,如果存在,则不会继续 ...
- 修改postgres密码
转载自:https://www.cnblogs.com/kaituorensheng/p/4735191.html 1. 修改PostgreSQL数据库默认用户postgres的密码 Postgr ...
- Centos6.5部署Sonar6.7.1备注
1.一定要用非root账号登录(自己建立账号),建立Sonar目录并部署,因为使用了Elasticsearch 5.6.3做搜索服务器,而它不允许用root账号启动服务,会报如下错误: Excepti ...
- python永久添加环境变量
import sys sys.path 系统环境是一个list,可以将自己需要的库添加进入,例如mysql库,hive库等等.有三种方式添加,均验证通过: 1 临时添加,在一个shell窗口中 ...
- bcdboot(引导修复工具) 命令行工具使用方法
BCDboot 是一种用于快速设置系统分区或修复系统分区上的启动环境的工具.系统分区是通过从已安装的 Windows(R) 映像复制一小部分启动环境文件来设置的.BCDboot 还会在系统分区上创建引 ...
- Sqluldr2 libclntsh.so报错处理
Sqluldr2 libclntsh.so报错处理 处理报错 [oracle@oracledg tmp]$ ./sqluldr2linux64.bin ./sqluldr2linux64.bin: e ...
- greendao3.2.0使用
源代码地址 https://github.com/greenrobot/greenDAO buildscript { repositories { jcenter() mavenCentral() } ...