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. QtNetwork说明(两)使用QT实现360的ctrl+ctrl特征

    头文字说明: <span style="font-size:18px;">#ifndef GOOGLESUGGEST_H #define GOOGLESUGGEST_H ...

  2. Asp.Net MVC页面静态化功能实现二:用递归算法来实现

    上一篇提到采用IHttpModule来实现当用户访问网站的时候,通过重新定义Response.Filter来实现将返回给客户端的html代码保存,以便用户下一次访问是直接访问静态页面. Asp.Net ...

  3. javascript立即调用的函数表达式N种写法(第二篇)

    原文:javascript立即调用的函数表达式N种写法(第二篇) 上一篇博客我谈到将函数声明转换为函数表达式最常见的一种写法是:通过括号()将匿名函数声明转换为函数表达式即(function(){}) ...

  4. 轨道sql谈话 dbms_monitor

    经常会遇到一个会话存在sql性能问题,但找不到哪一个sql导致DB更高的性能问题,这是我们需要在此session通过监测dbms_monitor软件包来实现. 首先确定要监控的会话sid及serial ...

  5. Talend open studio数据导入、导出、同步Mysql、oracle、sqlserver简单案例

    推荐大家一个BI工具:talend open studio.我也是刚接触,懂得不多,感觉比较神奇就想大家推荐一下... 由于公司项目,接触了一下BI工具talend,感觉功能很强大, 可以同步多种数据 ...

  6. jquery扩展方法案例

    -----------------扩展方法: $.extend({ "max": function (a, b) { if (a > b) return a; }, &quo ...

  7. 使用ServletConfig获得web.xml资源中的参数

    适用:一些不需要再Servlet文件中初始化的可以使用,例如:数据库用户名和密码 //Servlet文件 //实例化ServletConfig对象  ServletConfig servletConf ...

  8. Node填坑教程——HelloWorld

    环境安装(极简): Node需要的环境可以说及其简单,也可以说及其复杂.为什么这么说呢? 如果里只需要运行环境那么到Node官网下载一个包就行了.里面自带npm管理工具,这是包管理工具,以后会频繁的使 ...

  9. MVC 4

    Asp.net MVC 4 学习笔记(一) 公司最新的产品改成MVC开发模式了,以前产品开发都是经典的三层架构.对于“听闻”过而不有实际“品偿”过MVC的程序员来说,是知识的狭隘的表现.于是乎最近在学 ...

  10. SHELL编程笔记(二)之shell流程控制

    Shell控制流程结构 本章内容有:   退出状态   While.for和until loops循环   If then else语句   脚本中动作   菜单 条件控制语句 If then els ...