[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 ...
随机推荐
- pyecharts绘制地图可视化
pyecharts:官方文档 我们这里使用pyecharts模块进行绘图. pyecharts 项目包含了一系列的地理地图数据,这些数据或者已经内置,或者需要额外安装和加载,我们需要下载下面六个包. ...
- UVALive 4329 树状数组第二题
大白书上的题目,比较巧妙的是其分析,为了求某个i点做裁判的时候的情况数,只要知道左边有多少比它小的记为ansc,右边有多少比它小的记为ansd,则总种数,必定为 ansc*(右边总数-ansd)+an ...
- js获取指定日期n天之后的日期
function addDays(date, days,seperator='-') { let oDate = new Date(date).valueOf(); let nDate = oDate ...
- 吴裕雄--天生自然JAVA线程编程笔记:进程与线程
- Python笔记_第四篇_高阶编程_进程、线程、协程_4.协程
1.协程的概念: 子程序或者子函数,在所有语言中都是层级调用,比如A调用B,再B执行的过程中又可以调用C,C执行完毕返回,B执行返回,最后是A执行完毕返回.是通过栈来实现的,一个线程就是执行一个自称, ...
- 吴裕雄--天生自然 PHP开发学习:高级
<?php echo date("Y/m/d") . "<br>"; echo date("Y.m.d") . " ...
- 吴裕雄--天生自然 JAVA开发学习:变量类型
public class Variable{ static int allClicks=0; // 类变量 String str="hello world"; // 实例变量 pu ...
- 03 Mybatis:05.使用Mybatis完成CRUD
mybatis框架:共四天 明确:我们在实际开发中,都是越简便越好,所以都是采用不写dao实现类的方式.不管使用XML还是注解配置. 第二天:mybatis基本使用 mybatis的单表crud操作 ...
- 关于ebay平台接口(php)对接示例
获取订单接口示例 public function importEbayOrder(){ set_time_limit(0); if(empty( $this->_ShopApiEbay-> ...
- swoole使用协程
协程:协程可以理解为纯用户态的线程,其通过协作而不是抢占来进行切换.相对于进程或者线程,协程所有的操作都可以在用户态完成,创建和切换的消耗更低.Swoole可以为每一个请求创建对应的协程,根据IO的状 ...