[LeetCode]题解(python):039-Combination Sum
题目来源
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的更多相关文章
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [Leetcode][Python]39: Combination Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...
- LeetCode 039 Combination Sum
题目要求:Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique c ...
- 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 ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- 【LeetCode】039. Combination Sum
题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...
- 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 ...
- LeetCode笔记:39. Combination Sum
题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...
- leetcode第39题--Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- leetcode第38题--Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
随机推荐
- Hadoop科普文——常见的45个问题解答(CSDN)
Hadoop科普文——常见的45个问题解答 1.Hadoop集群可以运行的3个模式? 单机(本地)模式 伪分布式模式 全分布式模式 2. 单机(本地)模式中的注意点? 在单机模式(standalon ...
- POJ 3691 (AC自动机+状态压缩DP)
题目链接: http://poj.org/problem?id=3691 题目大意:给定N个致病DNA片段以及一个最终DNA片段.问最终DNA片段最少修改多少个字符,使得不包含任一致病DNA. 解题 ...
- javascript为元素绑定事件响应函数
javascript中为元素设置响应时间有两种方法. (1):object.onclick=functionName; 这种方法不可以传递参数. (2):object.onclick=function ...
- 【BZOJ】2946: [Poi2000]公共串
http://www.lydsy.com/JudgeOnline/problem.php?id=2946 题意:给n个串,求最大公共子串.(1<=n<=5,每个串长度<=2000) ...
- 【BZOJ】3039: 玉蟾宫(DP/单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=3039 每次看到我的提交都有点淡淡的忧伤T_T.. 看到此题我想到用前缀和维护点ij向左和向上能拓展的 ...
- HDU 4604 Deque(最长上升子序)
题目链接 本来就对N*log(N)算法不大会....然后各种跪了,求出最长不下降+最长不上升-最少相同元素.求相同元素,用二分求上界搞的.代码里4个二分.... #include <cstdio ...
- css 细节收集
细节1……………………………………………………………………………… 一.当文字与图片在一行,需要将文字与图片底对齐,需要这样写: <li>记住密码<img src="&qu ...
- 【转】个人对JQuery Proxy()函数的理解
原文地址:http://www.cnblogs.com/acles/archive/2012/11/20/2779282.html JQuery.proxy(function,context): 使用 ...
- multipart/form-data请求与文件上传
要上传文件,需要用post方法,并且设置enctype为multipart/form-data. <form action="/upload" method="po ...
- 实现Fragment 切换时不重新实例化
以前实现Fragment的切换都是用replace方法实现 public void startFragmentAdd(Fragment fragment) { FragmentManager frag ...