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. 分区格式化mkfs

    mkfs —  build a Linux filesystem 用法举例: mkfs.ext3  /dev/sdb1 #把sdb1格式化为ext3文件系统 也可以写成 mkfs -t ext3 /d ...

  2. zoj 1648 判断线段是否相交

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=648 Circuit Board Time Limit: 2 Second ...

  3. HDU 5833 Zhu and 772002

    HDU 5833 Zhu and 772002 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. So easy Webservice 8.spring整合CXF 发布WS

    1.添加jar包(cxf的jar包中包含了spring的jar包),添加spring配置文件 2.web.xml中配置CXFServlet,过滤WS服务的地址 <!-- 配置CXFServlet ...

  5. windows下的Git简单入手

    现在再搞golang,用go get github.com/xxx 命令使需要git.提交新项目到github.com也要git,老东西了,呵呵现在也要学习一下. 下载windows版的git. ·准 ...

  6. hdu 5587 规律

    题意:开始序列{1}; 一次变换{1,1,2}: 两次变换{1,1,2,1,2,2,3} ... 求s[n];题解:打表 S1,S2,S4,S8,S16,S32......公式 S[n]=S[最近的比 ...

  7. Java内存区域和判断对象“死”“活”算法

    转载自: http://www.cnblogs.com/aigongsi/archive/2012/04/06/2434771.html java与C,c++有很大的不同就是java语言开发者不需要关 ...

  8. 对sizeof的思考

    一.sizeof的特点(与strlen比较) 1.sizeof是运算符,strlen是函数,这意味着编译程序在编译的时候就把sizeof计算过了,所以sizeof(x)可以用来定义数组维数. 例如 i ...

  9. 一次tomcat服务器被入侵解决办法

    突然tomcat目录下莫名其妙的多了几个war文件,里面内容只有一个index.jsp,打开控制台发现多了几个应用,我可以确定不是我部署上去的,顺着应用访问竟然看到了

  10. git使用技巧

    git使用技巧 转载自:http://172.17.144.8/iceway.zhang/shares/201604/201604_git_tips.md.html 我们在工作中几乎每天都会用到git ...