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]]
class Solution(object):
def combinationSum3(self, k, n):
"""
:type k: int
:type n: int
:rtype: List[List[int]]
"""
ans = []
path = []
if k<=0 or n<=0:
return ans
self.comb_helper(k, n, 1, path, ans)
return ans def comb_helper(self, k, n, start, path, ans):
if n<0:
return
if n==0 and k==0:
ans.append(list(path))
return
for i in xrange(start, 10):
path.append(i)
self.comb_helper(k-1, n-i, i+1, path, ans)
path.pop()

216. Combination Sum III——本质DFS的更多相关文章

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

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

  2. [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 ...

  3. 【LeetCode】216. Combination Sum III

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

  4. LC 216. Combination Sum III

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

  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. 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 ...

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

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

  8. Leetcode 216. Combination Sum III

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

  9. LeetCode Combination Sum III (DFS)

    题意: 在1-9这9个数字中选择k个出来,若他们的和为n,则加入答案序列,注意升序. 思路: 用DFS的方式,每次决定一个数字,共决策k次.假设上个决策是第i位为5,那么i+1位的范围就是6-9. c ...

随机推荐

  1. CUBRID学习笔记 5 错误码

    服务器错误码 AS Error Code Number CAS Error Code Error Message Note -1000 CAS_ER_DBMS "CUBRID DBMS Er ...

  2. Expression Blend制作自定义按钮(转)

    来源:http://www.cnblogs.com/iChina/archive/2011/11/25/2262854.html Expression Blend制作自定义按钮 1.从Blend工具箱 ...

  3. Bootstrap日期和时间表单组件运用兼容ie8

    准备动作先到下载Bootstrap日期和时间组件. 1:引入bootstrap.min.css,因为bootstrap-datetimepicker里面的很多样式依赖bootstarp的主样式,字体文 ...

  4. linux查看文件夹大小

    du -sh 查看当前文件夹大小 du -sh * | sort -n 统计当前文件夹(目录)/文件的大小,并按文件大小排序 ------------------------------------- ...

  5. iOS开发之 Xcode 6 创建一个Empty Application

    参考链接http://jingyan.baidu.com/article/2a138328bd73f2074b134f6d.html Xcode 6 正式版如何创建一个Empty Applicatio ...

  6. 移动端 meta

    摘自http://www.cnblogs.com/shxydx/articles/2856882.html   控制显示区域各种属性: <meta content="width=dev ...

  7. Bootstrap强调相关的类

    在Bootstrap中除了使用标签<strong>.<em>等说明正文某些字词.句子的重要性,Bootstrap还定义了一套类名,这里称其为强调类名(类似前面说的“.lead” ...

  8. 用RestTemplate碰到的问题

    给请求加上头信息 Request request = new Request(); HttpHeaders requestHeaders = new HttpHeaders(); requestHea ...

  9. (转)springAOP解析-2

    原文地址:http://hzbook.group.iteye.com/group/wiki/2262-Spring 3.3.4  AOP拦截器链的调用在了解了对目标对象的直接调用以后,我们开始进入AO ...

  10. Canu Tutorial(canu指导手册)

    链接:Canu Tutorial Canu assembles reads from PacBio RS II or Oxford Nanopore MinION instruments into u ...