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 中的数字可以无限制 ...
随机推荐
- JS控制div跳转到指定的位置的几种解决方案总结
原文:http://www.jb51.net/article/96574.htm 这篇文章主要介绍了JS控制div跳转到指定的位置的几种解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考 ...
- 关于requests.exceptions.SSLError: HTTPSConnectionPool
问题: requests.exceptions.SSLError: HTTPSConnectionPool(host='mall.christine.com.cn', port=443): Max r ...
- Maven单独构建多模块项目中的单个模块
Maven单独构建多模块项目中的单个模块 说明: 1.可能存在的场景,多模块项目没有互相引用,那么此时可以单独构建单个项目,指定到子模块的pom.xml文件即可完成编译. 2.如果多模块项目各自都 ...
- Docker技术入门与实战 第二版-学习笔记-2-镜像构建
3.利用 commit 理解镜像构成 在之前的例子中,我们所使用的都是来自于 Docker Hub 的镜像. 直接使用这些镜像是可以满足一定的需求,而当这些镜像无法直接满足需求时,我们就需要定制这些镜 ...
- MP实战系列(十二)之封装方法详解(续二)
继续MP实战系列(十一)之封装方法详解(续一)这篇文章之后. 此次要讲的是关于查询. 查询是用的比较多的,查询很重要,好的查询,加上索引如鱼得水,不好的查询加再多索引也是无济于事. 1.selectB ...
- Android 在ScrollView中嵌入ViewPage后ViewPage不能很好的工作的问题解决
解决办法:重写ScrollView,如下代码所示: public class MyScrollView extends ScrollView{ private GestureDetector mGes ...
- vxlan 简单理解 vs calico 网络模型
1.vxlan(virtual Extensible LAN)虚拟可扩展局域网,是一种overlay的网络技术,使用MAC in UDP的方法进 行封装,共50字节的封装报文头. 2.VTEP为虚拟机 ...
- 20155202张旭 Exp6 信息收集与漏洞扫描
20155202张旭 Exp6 信息收集与漏洞扫描 一.实践目标与内容 1.实践目标: 掌握信息搜集的最基础技能. 具体有: 各种搜索技巧的应用 DNS IP注册信息的查询 基本的扫描技术:主机发现. ...
- 20155327 Exp9 Web安全基础
20155327 Exp9 Web安全基础 基础问题回答 (1)SQL注入攻击原理,如何防御 SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器 ...
- 20155334 《网络攻防》 Exp 8 Web基础
20155334 <网络攻防> Exp 8 Web基础 一.基础问题回答 1. 什么是表单? 表单在网页中主要负责数据采集功能,一个表单有三个基本组成部分: 部分 内容 表单标签 这里面包 ...