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 ...
随机推荐
- [Gamma] 发布说明
[Gamma] 发布说明 发布网址为http://60.205.230.0 新功能前瞻 团队合作:支持多人合作完成项目 项目进度管理:便于监控项目进度 站内信系统:团队合作与审核需要 已知BUG修复 ...
- Nginx 整合 Lua 实现动态生成缩略图
原文地址:Nginx 整合 Lua 实现动态生成缩略图 博客地址:http://www.extlight.com 一.前提 最近在开发一个项目,涉及到缩略图的功能,常见的生成缩略图的方案有以下几个: ...
- Python3.7 - Argparse模块的用法
argparse 是一个命令行参数解析模块. argparse 是python自带的命令行参数解析包,可以用来方便地读取命令行参数,当你的代码需要频繁地修改参数的时候,使用这个工具可以将参数和代码分离 ...
- Spring JDBC最佳实践(1)
原文地址:https://my.oschina.net/u/218421/blog/38513 Spring提供了两种使用JDBC API的最佳实践,一种是以JdbcTemplate为核心的基于Tem ...
- php 求商数和余数 的函数
//返回两数相除之商和余数function get_div_and_mod($left_operand, $right_operand){ $div = intval($left_operand / ...
- day60——单表操作补充(批量插入、查询、表结构)
day60 批量插入(bulk_create) # bulk_create obj_list = [] for i in range(20): obj = models.Book( title=f'金 ...
- HDU校赛 | 2019 Multi-University Training Contest 3
2019 Multi-University Training Contest 3 http://acm.hdu.edu.cn/contests/contest_show.php?cid=850 100 ...
- Java学习:面向对象三大特征:封装性、继承性、多态性之多态性。
面向对象三大特征:封装性.继承性.多态性之多态性. extends继承或者implemens实现,是多态性的前提. 例如:小菜是一个学生,但同时也是一个人.小菜是一个对象,这个对象既有学生形态,也有人 ...
- python matplotlib 设置x轴文本间隔显示(数字的话可以转为字符之后处理)
一个国际友人绘图遇到的问题,查了一手资料.主要参考的是这个老哥的做法(https://blog.csdn.net/wyquin/article/details/80508260) #totalSeed ...
- ftp搭建后外网无法连接和访问阿里云服务器(非软件)
阿里云服务器由于性价比高,是不少企业建站朋友们的首选.而在购买阿里云服务器后,不少客户反映其在搭建FTP后出现外网无法访问的问题,这里特意搜集整理了关于ftp搭建后外网无法连接和访问的问题,提供以下解 ...