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的更多相关文章

  1. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  2. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  3. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  4. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  5. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

  6. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  7. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  8. 【LeetCode】39. Combination Sum (2 solutions)

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  9. LC 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. 给普通用户加sudo权限

    系统环境:centos 7.0 引文:在实验室的服务器上给每个人分配了一个账号,但是有的时候普通用户需要使用root权限,比如装一些软件之类的.下面介绍怎么给普通用户添加sudo命令权限. 前提: s ...

  2. 零基础程序员入门Linux系统 !如何快速恢复系统?

    新手在学习Linux系统的时候,难免会遇到命令输错,或系统出错的难题.那么如何快速解决呢?本文就先给你一个后悔药,让你快速备份并恢复Linux系统.本文将以Ubuntu为例,在这之前,你需要一台服务器 ...

  3. selenium登陆qq邮箱页面

    from selenium import webdriver driver = webdriver.Chrome() driver.get('https://mail.qq.com/cgi-bin/l ...

  4. UML-领域模型的精化

    拙劣的分类和错误的概括是混乱生活的祸根.--H.G.Wells的总结 1.is-a原则 子类定义的成员变量.方法与超类必须一致.即:不能多,也不能少. 子类是“一种”超类.CreditPayment是 ...

  5. UML-什么是用例实现(场景实现)?

    1.总览图 解释: 用例--->领域模型 用例+领域模型--->设计模型(仅基于领域层的交互图) 2.什么是用例实现? 基于协作对象,如何在设计模型中实现某个用例.更确切的说是实现某个用例 ...

  6. 面试准备 HTTP协议

    http协议的主要特点 简单快速  //某个资源是固定的 (统一资源符)UII 灵活  //http头部有个数据类型,完成不同数据类型的传输 无连接  //链接一次就会断开 无状态 //客户端和服务端 ...

  7. Python 字典列表嵌套输入问题

  8. java数目

    第一部分: Java语言篇1 <Java编程规范>星级:适合对象:初级,中级介绍:作者James Gosling(Java之父),所以这本书我觉得你怎么也得读一下.对基础讲解的很不错. 2 ...

  9. 苹果浏览器移动端click事件延迟300ms的原因以及解决办法

    这要追溯至 2007 年初.苹果公司在发布首款 iPhone 前夕,遇到一个问题 —— 当时的网站都是为大屏幕设备所设计的.于是苹果的工程师们做了一些约定,应对 iPhone 这种小屏幕浏览桌面端站点 ...

  10. Python基础学习四

    Python基础学习四 1.内置函数 help()函数:用于查看内置函数的用途. help(abs) isinstance()函数:用于判断变量类型. isinstance(x,(int,float) ...