[Leetcode][Python]39: Combination Sum
# -*- 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的更多相关文章
- LeetCode题解39.Combination Sum
39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- 【LeetCode】39. Combination Sum (2 solutions)
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- 【LeetCode】39. Combination Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:[htt ...
- 【一天一道LeetCode】#39. Combination Sum
一天一道LeetCode系列 (一)题目 Given a set of candidate numbers (C) and a target number (T), find all unique c ...
- LeetCode OJ 39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
随机推荐
- Mongo客户端
http://www.linuxidc.com/Linux/2012-07/64233.htm http://www.oschina.net/p/rockmongo http://www.cnblog ...
- QT GUI(主)线程与子线程之间的通信——使用跨线程的信号槽
在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. 程序截图: 上代码: #include <QtGui> #inc ...
- C++14介绍
C++14标准是 ISO/IEC 14882:2014 Information technology -- Programming languages -- C++ 的简称[1] .在标准正式通过之 ...
- Opencv关于滑动条bar操作的实例
代码如下: //////////////////////////////////////////////////////////////////////// // // 该程序产生一个窗口10s 如果 ...
- UIPageViewController跳跃切换的问题
使用的是XHScrollMenu和UIPageViewController来构建5个页面: ViewController1, ViewController2, ViewController3, Vie ...
- android初级应用到高端架构教程------ 完整体系化学习android开发
系统的学习android开发技术,从应用到底层,再到架构,告别乱糟糟的学习方式,不再是抓不住重点.从上到下贯通,全面学习android开发.让你拥有清晰的思路,一步步学习android开发! 一般而言 ...
- CSS架构目标
擅长CSS的Web开发人员不仅可以从视觉上复制实物原型,还可以用代码进行完美的呈现.无需使用表格.尽可能少的使用图片.如果你是个名副其实的高手,你可以快速把最新和最伟大的技术应用到你的项目中,比如媒体 ...
- 10个原生JavaScript技巧
这些代码片段主要由网友们平时分享的作品代码里面和经常去逛网站然后查看源文件收集到的.把平时网站上常用的一些实用功能代码片段通通收集起来,方便网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作 ...
- SSCTF-Final-Re-Play
SSCTF-Final-Re-Play 比赛时花了一晚上搞定了,结果写脚本的时候发送的内容忘记base64加密然后异或8了,手动测试的时候当然是这样做了,写脚本的时候脑抽了.这个题只有forx做出来 ...
- ASP.NET MVC 学习之路-6
本文在于巩固基础 上文中使用的Code First创建数据库 本文将使用数据库生成模型 这里使用ADO.NET实体数据模型来生成模型 下面按照指导完成操作 下面看看如何使用这个框架 数据访问修改主要是 ...