[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 ...
随机推荐
- django_filter的values / values_list
from django.db import models class Blog(models.Model): name = models.CharField(max_length=100) tagli ...
- PHP 框架: CodeIgniter 分页教程
PHP 框架: CodeIgniter 分页教程 分类: PHP2009-04-23 11:09 3209人阅读 评论(0) 收藏 举报 框架phpbooksdatabaseurl数据库 目录(?)[ ...
- windows下使用mysqlbinlog做数据恢复时出现mysqlbinlog: File 'D:\MariaDB' not found (Errcode: 2)
出现如下这种情况是因为为找到bin-log日志,但为什么没有查到了??? 从图中可以看出系统只到到了D:\MariaDB但路径并没有查全,默认在windows下是以空格为分隔符的,所以他把D:\Mar ...
- No module named cv2 报错处理
运行python脚本出现“No module named cv2 ”,这时我们安装下opencv-python依赖即可 python3 -m pip install opencv-python
- 20个GitHub最热门的Java开源项目:文档、框架、工具
专注于Java领域优质技术,欢迎关注 文章来源:JavaGuide 以下涉及到的数据统计,数据来源:https://github.com/trending/java?since=monthly[1] ...
- 洛谷P1002 过河卒(动态规划)
题目描述 棋盘上 AA 点有一个过河卒,需要走到目标 BB 点.卒行走的规则:可以向下.或者向右.同时在棋盘上 CC 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为 ...
- MySQL--SHOW ENGINE INNODB STATUS
===================================== -- :: 0x7f305b965700 INNODB MONITOR OUTPUT =================== ...
- tomcat添加ssl证书
Tomcat支持JKS格式证书,从Tomcat7开始也支持PFX格式证书,两种证书格式任选其一. 文件说明: 1. 证书文件xxx.pem,包含两段内容,请不要删除任何一段内容. 2. 如果是证书系统 ...
- 吴裕雄--天生自然 PHP开发学习:面向对象
<?php class Site { /* 成员变量 */ var $url; var $title; /* 成员函数 */ function setUrl($par){ $this->u ...
- ubuntu19.10 相关软件安装
1.Typora Typora是一款Markdown撰写软件,Linux版本也相当好用. Typora的安装方式如下: # or run: # sudo apt-key adv --keyserver ...