摘要: 套dfs回溯模板,模板参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): # res作为返回值,设为全局变量 self.res = [] # 要得到3个a、2 阅读全文
posted @ 2020-11-23 17:00 人间烟火地三鲜 阅读(385) 评论(2) 推荐(1) 编辑
摘要: 套dfs回溯模板,模板参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): # res作为返回值,设为全局变量 self.res = [] def permut 阅读全文
posted @ 2020-11-23 16:59 人间烟火地三鲜 阅读(194) 评论(0) 推荐(1) 编辑
摘要: 代码思路:用三个set标记当前位置所在的行、主、次对角线方向上是否合法。 class Solution(object): def solveNQueens(self, n): """ :type n: int :rtype: List[List[str]] """ # 返回值 res = [] # 阅读全文
posted @ 2020-11-23 16:57 人间烟火地三鲜 阅读(249) 评论(0) 推荐(0) 编辑
摘要: class Solution(object): def solveNQueens(self, n): res = [] queen = [-1] * n column = set() dia1 = set() dia2 = set() self.dfs(n, res, queen, column, 阅读全文
posted @ 2020-11-23 16:50 人间烟火地三鲜 阅读(221) 评论(0) 推荐(0) 编辑
摘要: 代码一:DFS回溯 class Solution(object): # 方法一:DFS def allPathsSourceTarget(self, graph): """ :type graph: List[List[int]] :rtype: List[List[int]] """ if not 阅读全文
posted @ 2020-11-23 16:46 人间烟火地三鲜 阅读(208) 评论(0) 推荐(0) 编辑
摘要: 还是套回溯模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class CombinationIterator(object): def __init__(self, characters, combinationLength): 阅读全文
posted @ 2020-11-23 16:44 人间烟火地三鲜 阅读(164) 评论(0) 推荐(1) 编辑
摘要: 还是套模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def combinationSum3(self, k, n 阅读全文
posted @ 2020-11-23 16:40 人间烟火地三鲜 阅读(157) 评论(0) 推荐(0) 编辑
摘要: 还是套模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def combine(self, n, k): """ : 阅读全文
posted @ 2020-11-23 16:38 人间烟火地三鲜 阅读(117) 评论(0) 推荐(0) 编辑
摘要: 原串里有数字、也有字母,所以每选一个字符要加以判断,要是字母则有两种选择,数字就一种。 还是套模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): 阅读全文
posted @ 2020-11-23 16:36 人间烟火地三鲜 阅读(208) 评论(0) 推荐(0) 编辑
摘要: 套模板,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def subsetsWithDup(self, nums): "" 阅读全文
posted @ 2020-11-23 16:34 人间烟火地三鲜 阅读(71) 评论(0) 推荐(0) 编辑
摘要: 套模板,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def subsets(self, nums): """ :type 阅读全文
posted @ 2020-11-23 16:33 人间烟火地三鲜 阅读(94) 评论(0) 推荐(0) 编辑
摘要: 套模板,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def permuteUnique(self, nums): """ 阅读全文
posted @ 2020-11-23 16:31 人间烟火地三鲜 阅读(70) 评论(0) 推荐(0) 编辑
摘要: 套模板,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def permute(self, nums): """ :type 阅读全文
posted @ 2020-11-23 16:30 人间烟火地三鲜 阅读(92) 评论(0) 推荐(0) 编辑
摘要: 可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def combinationSum2(self, candidates, 阅读全文
posted @ 2020-11-23 16:28 人间烟火地三鲜 阅读(59) 评论(0) 推荐(0) 编辑
摘要: 可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def combinationSum(self, candidates, t 阅读全文
posted @ 2020-11-23 16:27 人间烟火地三鲜 阅读(91) 评论(0) 推荐(0) 编辑
摘要: 可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] self.mydict = {1: "", 2: "abc", 3: "de 阅读全文
posted @ 2020-11-23 16:25 人间烟火地三鲜 阅读(300) 评论(0) 推荐(0) 编辑
摘要: 参考:https://zhuanlan.zhihu.com/p/302415065 关于回溯 回溯是递归的“副产品”,并非高效算法,通过剪枝可以跳过非必要的搜索。 回溯算法能解决如下问题: 组合问题:N个数里面按一定规则找出k个数的集合; 排列问题:N个数按一定规则全排列,有几种排列方式; 切割问题 阅读全文
posted @ 2020-11-23 16:19 人间烟火地三鲜 阅读(331) 评论(0) 推荐(1) 编辑
摘要: # 一:术语、定义、特点 1、树 树是一种数据结构,它是由n(n>=1)个有限结点组成一个具有层次关系的集合。 树结构中的术语 节点的度:节点拥有的子树的数目; 叶子节点:度为0的节点; 分支结点:度不为0的节点; 树的度:树中节点的最大的度; 层次:根结点的层次为1,其余节点的层次等于该结点的双亲 阅读全文
posted @ 2020-06-08 14:51 人间烟火地三鲜 阅读(635) 评论(0) 推荐(0) 编辑
摘要: [TOC] 归并排序思路 1)找到单链表中间节点,从而将原链表分为左右两部分; 2)对左右两部分链表分别进行归并排序,并合并左右两部分; 3)分别对两部分重复上述操作,直到所有元素都已排序成功。 因为单链表只能从链表头节点向后遍历,第一步操作用快慢指针找链表中点的时间复杂度就为O(n)。由于之后都是 阅读全文
posted @ 2020-05-15 23:54 人间烟火地三鲜 阅读(321) 评论(0) 推荐(0) 编辑
摘要: [toc] 快速排序思路: 1)选定一个基准元素; 2)经过一趟排序,将所有元素分成两部分; 3)分别对两部分重复上述操作,直到所有元素都已排序成功。 因为单链表只能从链表头节点向后遍历,没有prev指针,因此必须选择头节点作为基准元素。这样第二步操作的时间复杂度就为O(n)。由于之后都是分别对两部 阅读全文
posted @ 2020-05-15 23:00 人间烟火地三鲜 阅读(220) 评论(0) 推荐(0) 编辑
摘要: class Solution(object): def totalNQueens(self, n): """ :type n: int :rtype: int """ res = [] queen = [-1] * n # column = [-1] * n # dia1 = [-1] * (2 * 阅读全文
posted @ 2020-11-23 16:53 人间烟火地三鲜 阅读(188) 评论(0) 推荐(0) 编辑
摘要: class Solution(object): def __init__(self): # 返回值,定义为全局变量 self.res = [] def partition(self, s): """ :type s: str :rtype: List[List[str]] """ if not s: 阅读全文
posted @ 2020-11-23 16:47 人间烟火地三鲜 阅读(196) 评论(0) 推荐(0) 编辑
摘要: 关于回溯,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def generateParenthesis(self, n): 阅读全文
posted @ 2020-11-23 16:42 人间烟火地三鲜 阅读(151) 评论(0) 推荐(0) 编辑