Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

这一题和combiantion sum I/II 其实很类似。只不过candidates只有[1,2,...,9]。而且只有当len(line) == k 并且 sum(line) = n才把line添加到res里面。

 class Solution(object):
def combinationSum3(self, k, n):
"""
:type k: int
:type n: int
:rtype: List[List[int]]
"""
nums = list(range(1,10))
res = []
self.helper(nums, k, n, res, [])
return res def helper(self, nums, k, target, res, line):
if target == 0 and len(line) == k:
res.append([x for x in line]) for i, x in enumerate(nums):
if x <= target:
line.append(x)
self.helper(nums[i+1:], k, target -x, res, line)
line.pop()

Leetcode 216. Combination Sum III的更多相关文章

  1. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. LeetCode 216. Combination Sum III (组合的和之三)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

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

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

  5. 【LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  6. 【刷题-LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  7. LC 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

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

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

  9. 【LeetCode】216. Combination Sum III 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:h ...

随机推荐

  1. java设计模式 策略模式Strategy

    本章讲述java设计模式中,策略模式相关的知识点. 1.策略模式定义 策略模式,又叫算法簇模式,就是定义了不同的算法族,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户.策略模式属于对象的 ...

  2. 人工智能与3A

    我在Tid2014上的一个小视频: 下一代的码农会是什么样的呢?且听咕咚老王的“3A”畅谈——“Ai.Art.Any”. 在艺术的视角下,世界是沉寂的.美丽的: 在码农的眼中,世界是有“码”的朦胧美吗 ...

  3. out

    //练习1 class Program { static void Main(string[] args) { //写一个方法 求一个数组中的最大值.最小值.总和.平均值 int[] numbers ...

  4. SSDB安装配置记录

    SSDB的性能很突出,与Redis基本相当了,Redis是内存型,容量问题是弱项,并且内存成本太高,SSDB针对这个弱点,使用硬盘存储,使用Google高性能的存储引擎LevelDB,适合大数据量处理 ...

  5. sql server之ROW_NUMBER() OVER()取每组的第N行数据

    先看个例子: document_id card_holder_id created_date document_type_id 1 1 2015-7-1 1 2 4 2015-7-2 1 3 4 20 ...

  6. Spark基本工作流程及YARN cluster模式原理(读书笔记)

    Spark基本工作流程及YARN cluster模式原理 转载请注明出处:http://www.cnblogs.com/BYRans/ Spark基本工作流程 相关术语解释 Spark应用程序相关的几 ...

  7. centos 进度条卡死

    CentOS 6.7 系统 在执行完删除更新包的全部操作之后, yum remove -y Deployment_Guide-en-US finger cups-libs cups ypbind &a ...

  8. Linux非root用户如何使用80端口启动程序

    默认情况下Linux的1024以下端口是只有root用户才有权限占用,我们的tomcat,apache,nginx等等程序如果想要用普通用户来占用80端口的话就会抛出java.net.BindExce ...

  9. OpenStack云计算快速入门之三:OpenStack镜像管理

    原文:http://blog.chinaunix.net/uid-22414998-id-3272059.html 第三部分 OpenStack镜像管理 一.简介 很多源都有为OpenStack已经编 ...

  10. postgreSQL使用

    1.1. 安装     自然,在你想开始使用 PostgreSQL 之前, 你必须安装它.PostgreSQL 很有可能 已经安装到你的节点上了,因为它包含在你的操作系统的发布里, 或者是系统管理员已 ...