[LC] 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]
] Time: O(N^|target|)
Space: O(|target|)
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
lst = []
self.dfs(candidates, lst, res, 0, target)
return res def dfs(self, candidates, lst, res, start, reminder):
if reminder < 0:
return
if reminder == 0:
res.append(list(lst))
return
for i in range(start, len(candidates)):
cur_num = candidates[i]
lst.append(cur_num)
self.dfs(candidates, lst, res, i, reminder - cur_num)
lst.pop()
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
if (candidates == null) {
return result;
}
List<Integer> list = new ArrayList<>();
helper(candidates, list, 0, target, result);
return result;
} private void helper(int[] nums, List<Integer> list, int index, int reminder, List<List<Integer>> result) {
if (reminder < 0) {
return;
}
if (reminder == 0) {
result.add(new ArrayList<>(list));
return;
}
for (int i = index; i < nums.length; i++) {
list.add(nums[i]);
helper(nums, list, i, reminder - nums[i], result);
list.remove(list.size() - 1);
}
}
}
[LC] 39. Combination Sum的更多相关文章
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- [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
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- 吴裕雄--天生自然 JAVASCRIPT开发学习:Number 对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 用CNN及MLP等方法识别minist数据集
用CNN及MLP等方法识别minist数据集 2017年02月13日 21:13:09 hnsywangxin 阅读数:1124更多 个人分类: 深度学习.keras.tensorflow.cnn ...
- SeetaFaceEngine系列1:Face Detection编译和使用
SeetaFace,根据GitHub上的介绍,就是一个开源的人脸检测.矫正和识别的开源库,是采用C++来编写的,并且是在CPU上执行的,没有用到GPU,但是可以用SSE或者OpenMP来加速.整个库分 ...
- 计算机utf-8/gbk/utf-16对照表
GBK UTF-16 UTF-8 ==================D2BB 4E00 E4 B8 80 一B6A1 4E01 E4 B8 81 丁C6DF 4E03 E4 B8 ...
- jsp动作标签学习
<jsp:useBean> <jsp:useBean>标签用于在指定的域范围内查找指定名称的JavaBean对象,如果存在则直接返回该JavaBean对象的引用,如果不存在则实 ...
- Codeforces Round #621 (Div. 1 + Div. 2)D dij(思维)
题:https://codeforces.com/contest/1307/problem/D 题意:给定无向图,n为点,m为边.在给个k,为特殊点的数目,题目要求在这些特殊点上连一条边,让新图最短路 ...
- springboot cloud 网盘
boot https://pan.baidu.com/s/12SkGJNu_M-I-pjg-GxqHRw 5uga boot-cloud https://pan.baidu.com/s/1gO ...
- Codeforces 1294C - Product of Three Numbers
题目大意: 给定一个n,问是否存在3个互不相同的,大于等于2的整数,满足a*b*c=n 解题思路: 可以把abc其中任意两个看作一个整体,例如a*b=d,那么可以发现d*c=n 所以d和c是n的因子 ...
- 创造新时代!谷歌、微软、Facebook等巨头推出全新数据计划的背后
对于所有互联网企业来说,用户及其数据都是最核心.最根本的宝贵财富.因此,每家互联网企业都不会轻易将自家的数据与别人分享.试想一下,阿里会将淘宝和天猫的数据共享给京东吗?腾讯会把QQ和微信的数据分享给微 ...
- python3.6内置模块——random详解
python内置模块random是用来生成随机数的,在许多场合都能应用到,算是比较常见的一种模块吧,下面详细介绍其具体用法. 基本用法 随机生成浮点数:有两种,一种没有参数,默认是0~1,另一种可以指 ...