1、首先对数组进行排序

2、递归搜索符合的元素

3、注意回溯

超越67%的用户

#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'author' import copy
class Solution(object): def __init__(self):
self.result = [] def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
""" candidates.sort(reverse = True)
return self.search_combinationSum(candidates, 0, [], 0, target) def search_combinationSum(self, candidates, index, tempRet, tempSum, target): #递归终止条件
if target == tempSum:
self.result.append(copy.deepcopy(tempRet))
return
elif target < tempSum:
return for i in range(index, len(candidates)):
if tempSum + candidates[i] <= target:
tempRet.append(candidates[i])
tempSum = tempSum + candidates[i]
self.search_combinationSum(candidates, i, tempRet, tempSum, target)
#回溯
del tempRet[len(tempRet) - 1]
tempSum = tempSum - candidates[i]
return self.result if __name__ == '__main__':
s = Solution()
print(s.combinationSum([2, 6, 3, 7], 7))

  

Leetcode-39-Submission Details (Medium)的更多相关文章

  1. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  2. 【leetcode】Submission Details

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  3. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  4. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  5. [Leetcode 39]组合数的和Combination Sum

    [题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...

  6. leetcode 39 dfs leetcode 40 dfs

    leetcode 39 先排序,然后dfs 注意先整全局变量可以减少空间利用 class Solution { vector<vector<int>>ret; vector&l ...

  7. LeetCode——Submission Details

    Description: Given a string of numbers and operators, return all possible results from computing all ...

  8. Submission Details [leetcode] 算法的改进

    最先看到这一题,直觉的解法就是len从1到s1.size()-1,递归调用比較s1和s2长度为len的子串是否相等.以及剩余部分是否相等. 将s1分成s1[len + rest],分别比較s2[len ...

  9. leetcode Submission Details

    代码: #include<iostream> #include<vector> using namespace std; struct ListNode { int val; ...

  10. [LeetCode] 39. Combination Sum 组合之和

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

随机推荐

  1. SQLCMD的介绍

    原文:SQLCMD的介绍 sqlcmd -S SERVERNAME -U USERNAME -P PASSWORD -i filename.sql 下面的内容是详细介绍sqlcmd的,有兴趣的朋友可以 ...

  2. 使用CountDownLatch和CyclicBarrier处理并发线程

    闲话不说,首先看一段代码: { IValueCallback remoteCallback = new IValueCallback.Stub() { <strong><span s ...

  3. 读书笔记—CLR via C#同步构造28-29章节

    前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...

  4. INSTEAD OF触发器

    Oracle触发器5(INSTEAD OF触发器) 前提:对于简单的视图,可以直接进行DML操作,但是对于复杂视图,不允许直接执行DML操作,当视图符合以下任何一种情况都不可以: 具有集合操作符(UN ...

  5. SHELL 近期学习

    由于项目中很少使用到shell脚本所以.只是偶尔自学一点.慢慢积累.下面就把近段时间积累的发出来.学习. #sort sort 按首字母排序 sort -n 按数字大小 从小到大排序 sort -rn ...

  6. DIP And DI

    依赖倒置(DIP)与依赖注入(DI)   依赖倒置原则(Dependency Inversion Principle)为我们提供了降低模块间耦合度的一种思路,依赖注入(Dependency Injec ...

  7. SZU:B47 Big Integer II

    Judge Info Memory Limit: 32768KB Case Time Limit: 10000MS Time Limit: 10000MS Judger: Normal Descrip ...

  8. JavaScript原生数组函数

    有趣的JavaScript原生数组函数 在JavaScript中,可以通过两种方式创建数组,构造函数和数组直接量, 其中后者为首选方法.数组对象继承自Object.prototype,对数组执行typ ...

  9. c# in deep 之使用匿名方法的内联委托操作

    匿名方法允许我们指定一个内联委托的操作,为创建委托实例表达式的一部分.其可以对代码进行极度精简,当然可读性变得很差.下面看一个求平方根的例子. List<int> list = new L ...

  10. twitter 授权过程

    转自:http://blog.csdn.net/yangjian8915/article/details/11816669 官方的流程图如下: 下面开始一步步讲解,如何获取最终的access_toke ...