[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 ...
随机推荐
- svn unable to connect to a repository at url 执行上下文错误 不能访问SVN服务器问题
1.检查visual SVN server 本地仓库服务是否正常启动 如果是SVN server service的的问题情形: (1).使用browser打开仓库位置,结果连接打不开. (2).查看s ...
- leetcode 326. Power of Three(不用循环或递归)
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a pow ...
- 使用ObjectAnimator开发打开、关闭书本动画
动画效果 动画效果-分享链接 (想做成gif图的,尝试各种工具无果) ObjectAnimator简单介绍及实现思路 ObjectAnimator是从api level 11 (Android3.0x ...
- Android EditText限制输入一些固定字符的属性
android:digits="abcdefghijklmnopqrstuvwxyz1234567890" 仅仅能输入这些
- UIPageViewController-浅析
一.UIPageViewController概念 控件为我们提供了一种像翻书效果的一种控件.我们可以通过使用UIPageViewController控件,来完成类似图书一样的翻页控制方式. 二 ...
- HDOJ3374 String Problem 【KMP】+【最小表示法】
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 用HTML5、地理定位API和Web服务来开发移动应用
HTML 5 是一项让人振奋的技术,这有着充分的理由.这将会是一次技术突破,因为它可以将桌面应用程序功能带入浏览器中.除了传统浏览器外,对于移动浏览器,其潜力甚至更大.不仅如此,最流行的移动浏览器甚至 ...
- T-SQL触发器
触发器 对表进行增删改操作时,自动执行的一个操作.当操作一个表的同时,需要进行其它关联操作的时候,就可以用到触发器. 比如:下订单时,创建中的商品数量需要减少 比如:退票时,总的票量要增加 属于DDL ...
- Android Studio无法关联Api23源码-提示Souces for android api 23 platform not found
最近升级了As,然后忽然就关联不上源码了,很不方便,找个Activity的源码都只有outline没有解释,还提示这个错误: Decompiled .class file, bytecode vers ...
- js提取整数部分,移除首末空格
给Object.prototype增加方法可使该方法对所有对象可用,这样的方式对函数.数组.字符串.数字.正则表达式和布尔值同样适用.比如说为Function.prototype增加方法来使得改方法对 ...