题目来源


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. java.lang.RuntimeException: Invalid action class configuration that references an unknown class named [xxxAction]。

    java.lang.RuntimeException: Invalid action class configuration that references an unknown class name ...

  2. 各新旧版本Java及其相关文档可以从这里下载

    http://www.oracle.com/technetwork/java/archive-139210.html

  3. USACO 5.4 Character Recognition(DP)

    非常恶心的一题,卡了三个月,没什么动力做了,代码直接抄的别人的... 这题主要思路就是预处理出几个数组,再预处理出几个数组,最后DP,输出一下路径... 写起来挺非常麻烦,代码不贴了,丢人... 把U ...

  4. 安卓学习笔记---Activity

    由于学期实训的要求,我开始学习安卓了.从本月一号开始,学了五天了.时间短,刚学到的东西容易忘,我记一下笔记. 首先是对Activity的理解.activity首先是一个java类,我们创建一个新的ac ...

  5. SQL优化之【类型转换】

    DBA的日常工作中SQL优化占大半的时间,通常都是SQL语句性能问题或者schema设计有问题,最近遇到一个类型转换的问题,所以在这里分享一下,废话不多说了,直接建表进行测试. mysql), key ...

  6. mysqld服务器如何查看使用变量的值

    你能用这个命令得到mysqld服务器缺省缓冲区大小: shell> mysqld --help 这个命令生成一张所有mysqld选项和可配置变量的表.输出包括缺省值并且看上去象这样一些东西: P ...

  7. /etc/named/named.conf.options中的Options参数

    listen-on port 53 { any; }; 监听在这部主机系统上面的哪个网路介面.预设是监听在localhost,亦即只有本机可以对DNS 服务进行查询,那当然是很不合理啊!所以这里要将大 ...

  8. Error 2147943712 during task creation

    In a Windows 2008 server, when you confirm the creation of a new task, you may obtain the following ...

  9. ElasticSearch实战-日志监控平台

    1.概述 在项目业务倍增的情况下,查询效率受到影响,这里我们经过讨论,引进了分布式搜索套件——ElasticSearch,通过分布式搜索来解决当下业务上存在的问题.下面给大家列出今天分析的目录: El ...

  10. JDBC链接数据库版本三,使用C3P0,使用jar文件两个

    JdbcUtil类: package com.xiaohui.jdbc.util; import java.sql.Connection; import java.sql.PreparedStatem ...