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 ...
随机推荐
- JavaScript中JSON字符串和JSON对象相互转化
JSON字符串转化为JSON对象的2种方式 一.使用函数eval var personsstr = '[{"Name":"zhangsan","Age ...
- js urlencode , encodeURIComponent
js 对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent ...
- JavaScript实例技巧精选(11)—计算器实例3
>>点击这里下载完整html源码<< 界面如下 将以下代码插入<body></body>中 <FORM NAME="Calc" ...
- 2014.3.12-C语言小测试
测试代码: 学号:1402049 1.请实现一个函数,功能为使用循环输出以下的图案 void print_alpha(int n) { int i, j; for(i=0;i<n;++i){ f ...
- 企业架构研究总结(31)——TOGAF架构内容框架之内容元模型(下)
2.2 治理扩展(Governance Extensions) 治理扩展元模型内容 治理扩展部分的意图在于引入额外的,并且与支持运营治理的目标和业务服务相关的结构化数据. 2.2.1 关注范围 为目标 ...
- Google Adsense(Google网站联盟)广告申请指南
Google AdSense 是一种获取收入的快速简便的方法,适合于各种规模的网站发布商.它可以在网站的内容网页上展示相关性较高的 Google 广告,并且这些广告不会过分夸张醒目.由于所展示的广告同 ...
- linux挂载分区
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda2 36G .3G 32G % / tmpf ...
- 街景地图 API
SOSO街景地图 API (Javascript)开发教程(1)- 街景 SOSO街景地图 Javascript API 干什么用的? 你想在网页里嵌入个地图,就需要它了! 另外,它还支持:地点搜 ...
- EasyUI DataGrid及Pagination
接上一篇EasyUI项目驱动学习 DataGrid数据表格及Pagination分页一起介绍 一.通过<table>标记创建DataGrid,嵌套<th>标签定义列表 < ...
- 在Visual Studio 2012中使用VMSDK开发领域特定语言1
在Visual Studio 2012中使用VMSDK开发领域特定语言(一) 前言 本专题主要介绍在Visual Studio 2012中使用Visualization & Modelin ...