# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 39: Combination Sum
https://oj.leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a target number (T),
find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times. Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3] ===Comments by Dabay===
递归。
先把candidates排序。
遍历candidates,用index来记录目前的指针。
''' class Solution:
# @param candidates, a list of integers
# @param target, integer
# @return a list of lists of integers
def combinationSum(self, candidates, target):
def combinationSum2(candidates, target, index, res, res_list):
if target == 0:
res_list.append(list(res))
return
while index < len(candidates) and target >= candidates[index]:
res.append(candidates[index])
combinationSum2(candidates, target-candidates[index], index, res, res_list)
res.pop()
index += 1 res_list = []
candidates.sort()
combinationSum2(candidates, target, 0, [], res_list)
return res_list def main():
sol = Solution()
candidates = [1,2]
target = 4
print sol.combinationSum(candidates, target) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]39: Combination Sum的更多相关文章

  1. LeetCode题解39.Combination Sum

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

  2. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

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

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

  4. 【LeetCode】39. Combination Sum 解题报告(Python & C++)

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

  5. 【一天一道LeetCode】#39. Combination Sum

    一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...

  6. LeetCode OJ 39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  7. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  8. 【一天一道LeetCode】#40. Combination Sum II

    一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...

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

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

随机推荐

  1. 变形课--hdu1181

    变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submis ...

  2. 实现Linux下的U盘(USB Mass Storage)驱动

    如何实现Linux下的U盘(USB Mass Storage)驱动 版本:v0.7 How to Write Linux USB MSC (Mass Storage Class) Driver Cri ...

  3. LoadRunner 技巧之THML 与 URL两种录制模式分析

    Loadrunner的Virtual User Generator 提供人脚本的录制功能,对于初学者来说,这大大的降低了编写脚本的门槛,loadrunner提供两种录制脚本的方式:Html_based ...

  4. Excel在任务栏中只显示一个窗口的解决办法

     Excel在任务栏中只显示一个窗口的解决办法  以前朋友遇到过这个问题,这次自己又遇到了,习惯了以前的那种在任务栏中显示全部窗口,方便用Alt+Tab键进行切换. 如果同时打开许多Excel工作簿, ...

  5. UIView添加事件

    UIView *loadView = [[UIControl alloc]initWithFrame:CGRectMake(0,0,320,480)]; loadView.backgroundColo ...

  6. 创立Est•Design华服高级成衣定制工作室 - 北京服装学院-莱佛士国际学院

    创立Est•Design华服高级成衣定制工作室 - 北京服装学院-莱佛士国际学院 创立Est•Design华服高级成衣定制工作室 创立Est•Design华服高级成衣定制工作室 童雪涛   " ...

  7. phonegap开发app中踩过的那些坑

    把遇到的问题列出来,假设有解决方式的,偶也会写下来.假设大家有更好解决方法的.欢迎留言噢 phonegap 2.9无法触发deviceready事件 亲们能够看下控制台有木有报错.假设有提示cordo ...

  8. MongoDB 操作

    通过CMD命令操作数据 确保MongoDB的服务已经正常安装,记下安装目录(D:\MongoDB\Service) 然后打开cmd 先转到D盘 cd D: 然后转到服务的安装目录下 cd D:\Mon ...

  9. maven将jar包安装到本地仓库的命令

    进入cmd 执行以下命令: mvn install:install-file -Dfile=E:\sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -Dar ...

  10. ipa 重签

    IPA 重签名 时间 2014-03-03 10:28:36  txx's blog原文  http://blog.rpplusplus.me/blog/2014/03/03/ipa-re-codes ...