题目来源


https://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.


题意分析


Input: a list as candidates, a value named target

Output:the list number that sumed to target

Conditions:在list里面找若干个数,使得和为target,注意每个数可以取若干次

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]


题目思路


先对list进行排序,然后穷举即可


AC代码(Python)

 _author_ = "YE"
# -*- coding:utf-8 -*- class Solution(object):
def find(self,candidates, target, start, valueList):
if target == 0:
Solution.ans.append(valueList)
length = len(candidates)
for i in range(start, length):
if candidates[i] > target:
return
self.find(candidates, target - candidates[i], i, valueList + [candidates[i]]) def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
candidates.sort()
Solution.ans = []
self.find(candidates, target, 0, [])
return Solution.ans s = Solution()
candidates = [2,3,6,7]
target = 7
print(s.combinationSum(candidates, target))

[LeetCode]题解(python):039-Combination Sum的更多相关文章

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

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

  2. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  3. LeetCode 039 Combination Sum

    题目要求:Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique c ...

  4. Leetcode 39 40 216 Combination Sum I II III

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

  5. LeetCode(40) Combination Sum II

    题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

  6. 【LeetCode】039. Combination Sum

    题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...

  7. Java for LeetCode 039 Combination Sum

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

  8. LeetCode笔记:39. Combination Sum

    题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...

  9. leetcode第39题--Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  10. leetcode第38题--Combination Sum

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

随机推荐

  1. BZOJ3648 : 寝室管理

    求环套外向树上节点数不小于K的路径数. 首先树的话直接点分治+树状数组$O(n\log^2n)$搞定 环套树的话,先删掉多余的边(a,b) 然后变成了一棵树,直接点分治 然后在树上找到a到b的路径,将 ...

  2. 详细讲解css单位px,em和rem的含义以及它们之间的区别

    一.首先介绍一下px px就是css中最基本的长度单位了,用px做单位基本上没什么问题,可以做到让页面按套路精确的展现! 可但是!但可是!如果全篇用px布局会暗藏一个蛋疼的问题,就是当用户和Ctrl滚 ...

  3. 百度搜索词&淘宝搜索词 接口实现

    百度和淘宝并没有正式的提供一个公开API给我们用,但是经过分析他们的源代码,还是找到了解决方法. 1 2 3 4 5 6 7 8 9 /*baidu&taobao callback*/ fun ...

  4. ios cocos2d FPS过低的解决方法

    每当运行程序时,左下角的FPS就低到了10,使app很卡, 原来程序主要卡的部分 -(void)draw{ NSDate *startTime = [NSDate date]; [self func] ...

  5. 【POJ】2954 Triangle(pick定理)

    http://poj.org/problem?id=2954 表示我交了20+次... 为什么呢?因为多组数据我是这样判断的:da=sum{a[i].x+a[i].y},然后!da就表示没有数据了QA ...

  6. FreeMarker教程

    一.什么是模板引擎,为什么要用模板引擎 在B/S程式设计中,常常有美工和程序员二个角色,他们具有不同专业技能:美工专注于表现——创建页面.风格.布局.效果等等可视元素:而程序员则忙于创建程式的商业流程 ...

  7. python表达式

    算术表达式: 地板除: >>> 10 // 3 3>>> 5 // 2 2>>> 5 // 31 取余: >>> 10 % 31 ...

  8. JQuery的定义

    JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF1.5+, Safari 2.0+, Opera ...

  9. 一些Office 365的问题收集

    1. 按照MS的最佳实践, 应该是先有本地域, 然后再有Office 365. 但是我们公司刚好相反, 于是按照前面的文章做完了硬关联, 但是现在发现对于那些原本就在Office 365上的用户的Pr ...

  10. 【产品更新】EVC8013 硬件更新!

    EVC8013 三合一磁耦合隔离转换器(USB转RS-232 / RS-485 / RS-422 ),迎来一次硬件大幅度升级,升级不加价! 本次升级主要有以下几个方面: 1.采用第二代金升阳 3000 ...