@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的更多相关文章

  1. Leetcode题库——39.组合总和

    @author: ZZQ @software: PyCharm @file: combinationSum.py @time: 2018/11/14 18:23 要求:给定一个无重复元素的数组 can ...

  2. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  3. Java实现 LeetCode 40 组合总和 II(二)

    40. 组合总和 II 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在 ...

  4. 40. 组合总和 II + 递归 + 回溯 + 记录路径

    40. 组合总和 II LeetCode_40 题目描述 题解分析 此题和 39. 组合总和 + 递归 + 回溯 + 存储路径很像,只不过题目修改了一下. 题解的关键是首先将候选数组进行排序,然后记录 ...

  5. 40组合总和II

    题目:给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一 ...

  6. 40. 组合总和 II leetcode JAVA

    题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使 ...

  7. LeetCode 40. 组合总和 II(Combination Sum II)

    题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能 ...

  8. leetcode 40. 组合总和 II (python)

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  9. 40. 组合总和 II

    题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只 ...

随机推荐

  1. BZOJ5092:[Lydsy1711月赛]分割序列(贪心,高维前缀和)

    Description 对于一个长度为n的非负整数序列b_1,b_2,...,b_n,定义这个序列的能量为:f(b)=max{i=0,1,...,n}((b_1 xor b_2 xor...xor b ...

  2. eclipse中文版官方下载

    目前eclipse的使用已经越来越广泛,它不仅应用于Java开发中,对于C++开发.php开发的程序员们也是非常喜爱.eclipse中文版下载其实是eclipse官方网站提供的中文包,默认情况下ecl ...

  3. datagridview 获取选中行的索引

    C# CODE for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)//遍历所有选中的行 { this.dataGrid ...

  4. CentOS常用命令备忘

    1. 查看进程 ps -a 杀掉进程 kill PID 2. 添加计划任务crontab -e 例如:30 21 * * * service httpd restart 每天21:30重启apache ...

  5. <Android 开源库> PhotoPicker 从头到脚

    1. 简介 PhotoPicker, 是一款开源的图片选择器.效果上和微信相似. 2. 使用方法 2.1 添加依赖 dependencies { compile 'me.iwf.photopicker ...

  6. 使用java实现hex和ascii码的转换

    几乎很少写JAVA代码,第一是确实不会,第二感觉JAVA写起来不爽(较python.golang),但总有万不得已必须要用java的时候.这里记录下使用java实现的hex十六进制和acsii码之间的 ...

  7. Docker学习4-Containers - 容器

    用Docker方式构建应用程序,从这个应用程序层次结构的底层容器开始.高于此级别的是一项服务,它定义了容器在生产中的行为方式.在顶层是堆栈,它定义了所有服务的交互. Stack  堆栈 Service ...

  8. Leetcode——121. 买卖股票的最佳时机

    题目描述:买卖股票的最佳时机 题目要求求解能获得最大利润的方式? 可以定一个二维数组 d [ len ] [ 2 ] ,其中d[ i ][ 0 ] 表示前i天可以获得的最大利润:d[ i ][ 1 ] ...

  9. vxlan 简单理解 vs calico 网络模型

    1.vxlan(virtual Extensible LAN)虚拟可扩展局域网,是一种overlay的网络技术,使用MAC in UDP的方法进 行封装,共50字节的封装报文头. 2.VTEP为虚拟机 ...

  10. php实现远程网络文件下载到服务器指定目录(方法一)

    PHP实现远程网络文件下载到服务器指定目录(方法一) <?php function getFile($url, $save_dir = '', $filename = '', $type = 0 ...