1、首先对数组进行排序

2、递归搜索符合的元素

3、注意回溯

超越67%的用户

  1. #!/usr/local/bin/python3
  2. # -*- coding: utf-8 -*-
  3. __author__ = 'author'
  4.  
  5. import copy
  6. class Solution(object):
  7.  
  8. def __init__(self):
  9. self.result = []
  10.  
  11. def combinationSum(self, candidates, target):
  12. """
  13. :type candidates: List[int]
  14. :type target: int
  15. :rtype: List[List[int]]
  16. """
  17.  
  18. candidates.sort(reverse = True)
  19. return self.search_combinationSum(candidates, 0, [], 0, target)
  20.  
  21. def search_combinationSum(self, candidates, index, tempRet, tempSum, target):
  22.  
  23. #递归终止条件
  24. if target == tempSum:
  25. self.result.append(copy.deepcopy(tempRet))
  26. return
  27. elif target < tempSum:
  28. return
  29.  
  30. for i in range(index, len(candidates)):
  31. if tempSum + candidates[i] <= target:
  32. tempRet.append(candidates[i])
  33. tempSum = tempSum + candidates[i]
  34. self.search_combinationSum(candidates, i, tempRet, tempSum, target)
  35. #回溯
  36. del tempRet[len(tempRet) - 1]
  37. tempSum = tempSum - candidates[i]
  38. return self.result
  39.  
  40. if __name__ == '__main__':
  41. s = Solution()
  42. 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. 1001 - Another A+B

    1001 - Another A+B Description Give you an integer a, you are to find two another integers which sum ...

  2. mvc+EF比较好的框架

    个人看了传智播客的一位讲师搭建的框架感觉很好,就自己通过模仿划了一下很不讲究的类图来学习之间的关系(有些地方可能有自己理解不对的地方).很感激那位讲师,我会把这个框架用在我自己的项目中.

  3. Effective C++(12) 复制对象时要复制每一个成员

    问题聚焦: 负责拷贝的两个操作:拷贝构造函数和重载赋值操作符. 一句话总结,确保被拷贝对象的所有成员变量都做一份拷贝. Demo   void logCall(const std::string&am ...

  4. iOS基础 - 控件属性

    一.控件的属性 1.CGRect frame 1> 表示控件的位置和尺寸(以父控件的左上角为坐标原点(0, 0)) 2> 修改这个属性,可以调整控件的位置和尺寸 2.CGPoint cen ...

  5. RDLC(Reportview)报表直接打印,支持所有浏览器,客户可在linux下浏览使用

    最近在做一个打印清单的,但是rdlc报表自带的工具栏中的打印按钮只有在ie内核下的浏览器才可以使用(其他的就会 隐藏),这导致了使用火狐和谷歌浏览器还有使用linux系统的客户打印成了问题,于是就自己 ...

  6. cocos2d-x-2.2.0_win7+vs2010

    cocos2d-x-2.2.0_win7+vs2010搭建_eclipse+ndk-r9+cygwin搭建_教程以及编译问题汇总 声明:我是才用c/c++和cocos2d-x的如果有错误欢迎指出 文章 ...

  7. .NET核心代码保护策略

    .NET核心代码保护策略-隐藏核心程序集 经过之前那个道德指责风波过后也有一段时间没写博客了,当然不是我心怀内疚才这么久不写,纯粹是程序员的通病..怎一个懒字了得,本来想写一些长篇大论反讽一下那些道德 ...

  8. IOS使用不同父类的 view 进行约束

    最终效果图如下: 很多限制条件都已经应用到了视图中,我们在解释一下: ·在我们的视图控制器的主视图中有两个灰色的视图.两个视图距视图控制器的视图左 边和右边都有一个标准的空白距离.视图的顶部距顶部的视 ...

  9. ssdt_hook NtOpenProcess

        获取ssdt表中所有函数的地址 for (int i = 0; i < KeServiceDescriptorTable->NumberOfServices; i++) {     ...

  10. 使用entity framework开发oracle

    A.vs2010 SP1 B.ODAC(http://www.oracle.com/technetwork/database/windows/downloads/index-101290.html) ...