Leetcode题库——40.组合总和II
@author: ZZQ
@software: PyCharm
@file: combinationSum2.py
@time: 2018/11/15 18:38
要求:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用一次。
说明:
所有数字(包括目标数)都是正整数。
解集不能包含重复的组合。
示例 1:
输入: candidates = [10,1,2,7,6,1,5], target = 8,
所求解集为:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
示例 2:
输入: candidates = [2,5,2,1,2], target = 5,
所求解集为:
[
[1,2,2],
[5]
]
思路; 深搜+ 减枝
注意停止: 如果当前temp_ans > target, 则停止, return
注意每个元素只能使用一次, 先对数组进行排序,然后每次深搜都从下一个元素开始。
注意拷贝temp_ans到新的数组中,再存入ans中。
import copy
class Solution():
def __init__(self):
pass
def combinationSum2(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 = -1
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+1, len(candidates)):
if i > start_index+1 and candidates[i] == candidates[i-1]:
continue
temp_ans.append(candidates[i])
self.dfs(temp_ans, temp_sum + candidates[i], i, target, candidates, ans)
temp_ans.pop()
Leetcode题库——40.组合总和II的更多相关文章
- Leetcode题库——39.组合总和
@author: ZZQ @software: PyCharm @file: combinationSum.py @time: 2018/11/14 18:23 要求:给定一个无重复元素的数组 can ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- Java实现 LeetCode 40 组合总和 II(二)
40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...
- 40. 组合总和 II + 递归 + 回溯 + 记录路径
40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...
- 40组合总和II
题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...
- 40. 组合总和 II leetcode JAVA
题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使 ...
- LeetCode 40. 组合总和 II(Combination Sum II)
题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...
- leetcode 40. 组合总和 II (python)
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...
- 40. 组合总和 II
题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只 ...
随机推荐
- python第三十五课——生成器
1.生成器: 什么是生成器? 它内部封装了一套公式/算法,只有等到需要调用/执行数据时 --> next()函数执行 才会将公式计算得到数据结果,这就是生成器的原理(核心思想): [注意事项]: ...
- 当我们跑SparkSQL时候为了更好地了解SparkSQL运行,可以WEBUI看SQL的Tab
- Docker介绍-hc课堂笔记
1,传统模式-多个服务器:申请.安装jdk等.部署环境. 容器-整包,把有东西打包到一起,把这个包放在服务器上. linux中装了docker,起100个服务,改个数字就可以,5分钟左右. 2,虚拟化 ...
- jenkins自动部署到tomcat报错:ERROR: Publisher hudson.plugins.deploy.DeployPublisher aborted due to exception
参考地址: http://blog.csdn.net/weiguang1017/article/details/9011353 manager-script — Access to the tools ...
- css 字体、文本、padding的样式
一.字体的样式: 1)字体倾斜:font-style:italic 2)字体大小:font-size 一般为偶数. 3)行高:line-height 当行高为奇数的时候,是文字上面比文字下面的少一 ...
- 03-Maven坐标管理
1.什么是坐标? 2.坐标的详细概念 3.Maven包引用
- Delphi DBGrid类控件定位到某一行,并更改为选中状态。
Delphi中,可以使用数据集控件提供的 Locate 成员方法快速定位至某条记录, 然后通过清除数据集控件的选中状态,并重新赋值达到我们的目的. grDirectory.DataSource.Dat ...
- 在ListBoxItem的样式中的button传参,把当前选中项传递到命令的方法
原文:在ListBoxItem的样式中的button传参,把当前选中项传递到命令的方法 前端页面: <Style x:Key="ThumbItemStyle" TargetT ...
- Android开发——Android进程保活招式大全
)前台进程(Foreground process),即用户当前操作所必需的进程,通常数量不多.举例如下: //拥有用户正在交互的 Activity(已调用 onResume()) //拥有某个 Ser ...
- Qt FFMPEG+OpenCV开启摄像头
//ffmpegDecode.h #ifndef __FFMPEG_DECODE_H__ #define __FFMPEG_DECODE_H__ #include "global.h&quo ...