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 ...
随机推荐
- ASP.NET MVC进阶
ASP.NET MVC进阶 一.ASP.NET MVC中的AJAX应用 首先,在ASP.NET MVC中使用自带的ajax功能,必须要导入2个js文件(顺序不能颠倒): ASP.NET MVC提供了2 ...
- Scala 的 Web 框架 Lift 开始 3.0 版本开发
Scala 的 Web 框架 Lift 开始 3.0 版本开发 http://demo.liftweb.net/ http://liftweb.net/download Lift 框架在不断的成长和改 ...
- 解决easyui datagrid load时缓存问题
修改easyui datagrid内容保存后,使用$("#dg").datagrid("reload");或者$("#dg").datagr ...
- .net图片快速去底(去除白色背景)
public System.Drawing.Bitmap KnockOutGzf(String path) { System.Drawing.Image image = System.Drawing. ...
- ASP.NET MVC IOC之Unity攻略
ASP.NET MVC IOC之Unity攻略 一.你知道IOC与DI吗? 1.IOC(Inversion of Control )——控制反转 即依赖对象不在被依赖模块的类中直接通过new来获取 先 ...
- windows服务安装启动报错误1053:服务没有及时响应启动或控制请求
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0&qu ...
- 调式WP程序报0x80131500错误的解决办法
在虚拟机上安装了win8系统和VS2013,但是在允许第一个WP程序时,居然报0x80131500错误信息,经查询原来是VS2013需更新的问题,如果你用的是VS2012,但是又系统升级到了win8. ...
- JSP技术模型(五)JSP隐含变量
在JSP页面的转换阶段,容器在_jspService()方法中申明并初始化一些变量,可以在JSP页面小脚本中或表达式中直接使用这些变量. 一.JSP页面中可使用的隐含变量 1.applicationj ...
- Redis一些命令总结
链接操作相关的命令 quit:关闭连接(connection) auth:简单密码认证 持久化 save:将数据同步保存到磁盘 bgsave:将数据异步保存到磁盘 lastsave:返回上次成功将 ...
- CSLA .NET是一个.NET软件开发框架
CSLA .NET是一个.NET软件开发框架,帮助开发者“为Windows.Web.面向服务和工作流等应用构建强大和可维护的业务逻辑层”. CSLA是Component-based, Scalable ...