dfs --path sum 问题 本质上就是组合问题(有去重)
135. 数字组合
给定一个候选数字的集合 candidates 和一个目标值 target. 找到 candidates 中所有的和为 target 的组合.
在同一个组合中, candidates 中的某个数字不限次数地出现.
样例
样例 1:
输入: candidates = [2, 3, 6, 7], target = 7
输出: [[7], [2, 2, 3]]
样例 2:
输入: candidates = [1], target = 3
输出: [[1, 1, 1]]
注意事项
- 所有数值 (包括
target) 都是正整数. - 返回的每一个组合内的数字必须是非降序的.
- 返回的所有组合之间可以是任意顺序.
- 解集不能包含重复的组合.
class Solution:
"""
@param candidates: A list of integers
@param target: An integer
@return: A list of lists of integers
"""
def combinationSum(self, candidates, target):
# write your code here
result = []
self.dfs(sorted(list(set(candidates))), target, result, path=[], start_index=0)
return result def dfs(self, nums, target, result, path, start_index):
if target == 0 and path:
result.append(list(path)) if target < 0:
return for i in range(start_index, len(nums)):
path.append(nums[i])
self.dfs(nums, target-nums[i], result, path, i)
path.pop()
153. 数字组合 II
给定一个数组 num 和一个整数 target. 找到 num 中所有的数字之和为 target 的组合.
样例
样例 1:
输入: num = [7,1,2,5,1,6,10], target = 8
输出: [[1,1,6],[1,2,5],[1,7],[2,6]]
样例 2:
输入: num = [1,1,1], target = 2
输出: [[1,1]]
解释: 解集不能包含重复的组合
注意事项
- 在同一个组合中,
num中的每一个数字仅能被使用一次. - 所有数值 (包括
target) 都是正整数. - 返回的每一个组合内的数字必须是非降序的.
- 返回的所有组合之间可以是任意顺序.
- 解集不能包含重复的组合.
class Solution:
"""
@param num: Given the candidate numbers
@param target: Given the target number
@return: All the combinations that sum to target
"""
def combinationSum2(self, nums, target):
# write your code here
result = []
self.dfs(sorted(nums), target, result, path=[], start_index=0)
return result def dfs(self, nums, target, result, path, start_index):
if target == 0 and path:
result.append(list(path)) if target < 0:
return for i in range(start_index, len(nums)):
if i > 0 and nums[i] == nums[i-1] and i > start_index:
continue
path.append(nums[i])
self.dfs(nums, target-nums[i], result, path, i+1)
path.pop()
90. k数和 II
给定n个不同的正整数,整数k(1<= k <= n)以及一个目标数字。
在这n个数里面找出K个数,使得这K个数的和等于目标数字,你需要找出所有满足要求的方案。
样例
样例 1:
输入: [1,2,3,4], k = 2, target = 5
输出: [[1,4],[2,3]]
样例 2:
输入: [1,3,4,6], k = 3, target = 8
输出: [[1,3,4]]
这个题目更简单,直接dfs模板即可做。
class Solution:
"""
@param: A: an integer array
@param: k: a postive integer <= length(A)
@param: targer: an integer
@return: A list of lists of integer
"""
def kSumII(self, A, k, target):
# write your code here
path = []
result = []
self.dfs(A, k, target, path, result, start_index=0)
return result def dfs(self, arr, k, target, path, result, start_index):
if target == 0 and k == 0:
result.append(list(path))
return if target < 0 or k <= 0:
return for i in range(start_index, len(arr)):
path.append(arr[i])
self.dfs(arr, k-1, target-arr[i], path, result, i+1)
path.pop()
dfs --path sum 问题 本质上就是组合问题(有去重)的更多相关文章
- leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 113. Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- 【LeetCode】113. Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- Path Sum II 总结DFS
https://oj.leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf p ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
随机推荐
- 解决docker容器日志导致主机磁盘空间满了的情况
日志文件在 /var/lib/docker/containers/<docker_container_id>/ 目录下 查看日志大小 vim /opt/docker_log_siz ...
- Collection 接口的 toArray 方法
Collection 接口的 toArray 方法 方法签名 Object[] toArray() 返回包含此 collection 中所有元素的数组. T[] toArray(T[] a) 返回包含 ...
- kubenetes之配置pod的QoS
系列目录 上节提到过,QoS影响pod的调度和驱离,本节讲解如何通过配置pod来使它自动被赋予一个QoS 实际上是pod的配置达到一定标准,则kubernetes会自动为其它添加一个QoS类 QoS类 ...
- java运算符的优先级别
一.运算符的优先级 运算符按照优先级别的高低排序分别是:自加/减运算符. 算术运算符.比较运算符.逻辑运算符.赋值运算符.具体请参考下表: 顺序 运算符 1. 括号,如 ( ) 和 [ ] 2. 一元 ...
- ASP.NET Core中如何显示[PII is hidden]的隐藏信息
有时候我们在ASP.NET Core项目运行时,发生在后台程序中的错误会将关键信息隐藏为[PII is hidden]这种占位符,如下所示: 而知道这些关键信息,有时候对我们调试程序是非常重要的.所以 ...
- 关闭Postman 证书的验证
1.问题背景 使用自己生成的SSL证书,用Postman访问失败.需要忽略SSL证书的验证 2.关闭Postman 证书的验证 在Settings-General中 关闭SSL certificate ...
- C# EventHandler观察者模式
C#和java比较: java中使用的是接口.C#使用委托机制,可以用时 + 运算符进行注册,直接多播. 而java中是一般是使用一个集合来保存观察者. 发布者(Publisher)= 被观察者 (O ...
- Java自学-接口与继承 对象转型
Java中的对象转型 示例 1 : 明确引用类型与对象类型的概念 首先,明确引用类型与对象类型的概念 在这个例子里,有一个对象 new ADHero(), 同时也有一个引用ad 对象是有类型的, 是A ...
- 【MySQL】mysql中的锁机制
一.分类 MySQL的锁机制不同的存储引擎支持不同的锁机制,分为表级锁.行级锁.页面锁.MyISAM和MEMORY存储引擎采用的是表级锁(table-level locking):BDB存储引擎采用的 ...
- css 平行四边形
平行四边形 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...