Leetcode-39-Submission Details (Medium)
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)的更多相关文章
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- 【leetcode】Submission Details
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- [Leetcode 39]组合数的和Combination Sum
[题目] Given a set of candidate numbers (candidates) (without duplicates) and a target number (target) ...
- leetcode 39 dfs leetcode 40 dfs
leetcode 39 先排序,然后dfs 注意先整全局变量可以减少空间利用 class Solution { vector<vector<int>>ret; vector&l ...
- LeetCode——Submission Details
Description: Given a string of numbers and operators, return all possible results from computing all ...
- Submission Details [leetcode] 算法的改进
最先看到这一题,直觉的解法就是len从1到s1.size()-1,递归调用比較s1和s2长度为len的子串是否相等.以及剩余部分是否相等. 将s1分成s1[len + rest],分别比較s2[len ...
- leetcode Submission Details
代码: #include<iostream> #include<vector> using namespace std; struct ListNode { int val; ...
- [LeetCode] 39. Combination Sum 组合之和
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
随机推荐
- 1001 - Another A+B
1001 - Another A+B Description Give you an integer a, you are to find two another integers which sum ...
- mvc+EF比较好的框架
个人看了传智播客的一位讲师搭建的框架感觉很好,就自己通过模仿划了一下很不讲究的类图来学习之间的关系(有些地方可能有自己理解不对的地方).很感激那位讲师,我会把这个框架用在我自己的项目中.
- Effective C++(12) 复制对象时要复制每一个成员
问题聚焦: 负责拷贝的两个操作:拷贝构造函数和重载赋值操作符. 一句话总结,确保被拷贝对象的所有成员变量都做一份拷贝. Demo void logCall(const std::string&am ...
- iOS基础 - 控件属性
一.控件的属性 1.CGRect frame 1> 表示控件的位置和尺寸(以父控件的左上角为坐标原点(0, 0)) 2> 修改这个属性,可以调整控件的位置和尺寸 2.CGPoint cen ...
- RDLC(Reportview)报表直接打印,支持所有浏览器,客户可在linux下浏览使用
最近在做一个打印清单的,但是rdlc报表自带的工具栏中的打印按钮只有在ie内核下的浏览器才可以使用(其他的就会 隐藏),这导致了使用火狐和谷歌浏览器还有使用linux系统的客户打印成了问题,于是就自己 ...
- cocos2d-x-2.2.0_win7+vs2010
cocos2d-x-2.2.0_win7+vs2010搭建_eclipse+ndk-r9+cygwin搭建_教程以及编译问题汇总 声明:我是才用c/c++和cocos2d-x的如果有错误欢迎指出 文章 ...
- .NET核心代码保护策略
.NET核心代码保护策略-隐藏核心程序集 经过之前那个道德指责风波过后也有一段时间没写博客了,当然不是我心怀内疚才这么久不写,纯粹是程序员的通病..怎一个懒字了得,本来想写一些长篇大论反讽一下那些道德 ...
- IOS使用不同父类的 view 进行约束
最终效果图如下: 很多限制条件都已经应用到了视图中,我们在解释一下: ·在我们的视图控制器的主视图中有两个灰色的视图.两个视图距视图控制器的视图左 边和右边都有一个标准的空白距离.视图的顶部距顶部的视 ...
- ssdt_hook NtOpenProcess
获取ssdt表中所有函数的地址 for (int i = 0; i < KeServiceDescriptorTable->NumberOfServices; i++) { ...
- 使用entity framework开发oracle
A.vs2010 SP1 B.ODAC(http://www.oracle.com/technetwork/database/windows/downloads/index-101290.html) ...