Leetcode题库——39.组合总和
@author: ZZQ
@software: PyCharm
@file: combinationSum.py
@time: 2018/11/14 18:23
要求:给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的数字可以无限制重复被选取。
说明:
所有数字(包括 target)都是正整数。
解集不能包含重复的组合。
示例 1:
输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]
示例 2:
输入: candidates = [2,3,5], target = 8,
所求解集为:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
思路:深搜+减枝
注意停止搜索:当和大于target时,结束该条支路的搜索。
注意去重:先对数组排好序,每次都处理当前元素以后的元素。
注意: 存入ans时需要将临时数组拷贝出来,否则ans中的答案会因为temp_ans的改变而一直改变。
import copy
class Solution():
def __init__(self):
pass
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
candidates.sort()
can_len = len(candidates)
if can_len == 0:
return []
ans = []
temp_ans = []
temp_sum = 0
start_index = 0
self.dfs(temp_ans, temp_sum, start_index, target, candidates, ans)
return ans
def dfs(self, temp_ans, temp_sum, start_index, target, candidates, ans):
if temp_sum == target:
tt_ans = copy.deepcopy(temp_ans)
ans.append(tt_ans)
return
if temp_sum > target:
return
for i in range(start_index, len(candidates)):
temp_ans.append(candidates[i])
self.dfs(temp_ans, temp_sum + candidates[i], i, target, candidates, ans)
temp_ans.pop()
Leetcode题库——39.组合总和的更多相关文章
- Leetcode题库——40.组合总和II
@author: ZZQ @software: PyCharm @file: combinationSum2.py @time: 2018/11/15 18:38 要求:给定一个数组 candidat ...
- Java实现 LeetCode 39 组合总和
39. 组合总和 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字 ...
- [leetcode] 39. 组合总和(Java)(dfs、递归、回溯)
39. 组合总和 直接暴力思路,用dfs+回溯枚举所有可能组合情况.难点在于每个数可取无数次. 我的枚举思路是: 外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(c ...
- 【LeetCode】39. 组合总和
39. 组合总和 知识点:递归:回溯:组合:剪枝 题目描述 给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 ...
- [LeetCode] 39. 组合总和
题目链接 : https://leetcode-cn.com/problems/combination-sum/ 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ...
- leetcode题库
leetcode题库 #题名题解通过率难度出现频率 1 两数之和 46.5%简单2 两数相加 35.5%中等3 无重复字符的最长子串 31.1%中等4 寻找两个有序数组的中位 ...
- leetcode刷题-39组合总和
题目 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重 ...
- Leetcode题目39.组合总和(回溯+剪枝-中等)
题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无 ...
- leetcode 39 组合总和 JAVA
题目: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制 ...
随机推荐
- 关于Vue的nextTick的一点小理解
官方文档表示:为了在数据变化之后等待Vue完成更新DOM,可以在数据变化之后立即执行Vue.$nextTick(callback),这样回调函数就可以在数据变化之后立即执行. 这段话的意思是: 例如: ...
- Centos7 Nginx 开机启动
Centos 系统服务脚本目录: 用户(user) 用户登录后才能运行的程序,存在用户(user) /usr/lib/systemd/ 系统(system) 如需要开机没有登陆情况下就能运行的程序,存 ...
- 如何取消idea中的本地源码关联
- header头
<?php header('HTTP/1.1 200 OK'); // ok 正常访问 header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在 heade ...
- stl vector、红黑树、set、multiset、map、multimap、迭代器失效、哈希表(hash_table)、hashset、hashmap、unordered_map、list
stl:即标准模板库,该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法 六大组件: 容器.迭代器.算法.仿函数.空间配置器.迭代适配器 迭代器:迭代器(iterator)是一种抽象的设计 ...
- [转]VS 2012环境下使用MFC进行OpenGL编程
我就不黏贴复制了,直接给出原文链接:VS 2012环境下使用MFC进行OpenGL编程 其它好文链接: 1.OpenGL系列教程之十二:OpenGL Windows图形界面应用程序
- Posts Tagged ‘This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register问题的解决办法
HowTo Install redhat package with YUM command without RHN February 26, 2014 in Redhat / Linux Tips a ...
- ICSharpCode.SharpZipLib 开源压缩库使用示例
官方网站:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 插件描述: ICSharpCode.SharpZipLib.dl ...
- Net Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程
在Asp.Net Core 中使用外部登陆(google.微博...) 原文出自Rui Figueiredo的博文<External Login Providers in ASP.NET C ...
- 模拟赛 sutoringu
sutoringu 题意: 询问有多少一个字符串内有多少个个子区间,满足可以分成k个相同的串. 分析: 首先可以枚举一个长度len,表示分成的k个长为len的串.然后从1开始,每len的长度分成一块, ...