摘要:
套dfs回溯模板,模板参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): # res作为返回值,设为全局变量 self.res = [] # 要得到3个a、2 阅读全文
摘要:
套dfs回溯模板,模板参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): # res作为返回值,设为全局变量 self.res = [] def permut 阅读全文
摘要:
代码思路:用三个set标记当前位置所在的行、主、次对角线方向上是否合法。 class Solution(object): def solveNQueens(self, n): """ :type n: int :rtype: List[List[str]] """ # 返回值 res = [] # 阅读全文
摘要:
class Solution(object): def totalNQueens(self, n): """ :type n: int :rtype: int """ res = [] queen = [-1] * n # column = [-1] * n # dia1 = [-1] * (2 * 阅读全文
摘要:
class Solution(object): def solveNQueens(self, n): res = [] queen = [-1] * n column = set() dia1 = set() dia2 = set() self.dfs(n, res, queen, column, 阅读全文
摘要:
class Solution(object): def __init__(self): # 返回值,定义为全局变量 self.res = [] def partition(self, s): """ :type s: str :rtype: List[List[str]] """ if not s: 阅读全文
摘要:
代码一:DFS回溯 class Solution(object): # 方法一:DFS def allPathsSourceTarget(self, graph): """ :type graph: List[List[int]] :rtype: List[List[int]] """ if not 阅读全文
摘要:
还是套回溯模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class CombinationIterator(object): def __init__(self, characters, combinationLength): 阅读全文
摘要:
关于回溯,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def generateParenthesis(self, n): 阅读全文
摘要:
还是套模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def combinationSum3(self, k, n 阅读全文
摘要:
还是套模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def combine(self, n, k): """ : 阅读全文
摘要:
原串里有数字、也有字母,所以每选一个字符要加以判断,要是字母则有两种选择,数字就一种。 还是套模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): 阅读全文
摘要:
套模板,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def subsetsWithDup(self, nums): "" 阅读全文
摘要:
套模板,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def subsets(self, nums): """ :type 阅读全文
摘要:
套模板,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def permuteUnique(self, nums): """ 阅读全文
摘要:
套模板,可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def permute(self, nums): """ :type 阅读全文
摘要:
可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def combinationSum2(self, candidates, 阅读全文
摘要:
可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] def combinationSum(self, candidates, t 阅读全文
摘要:
可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): self.res = [] self.mydict = {1: "", 2: "abc", 3: "de 阅读全文
摘要:
参考:https://zhuanlan.zhihu.com/p/302415065 关于回溯 回溯是递归的“副产品”,并非高效算法,通过剪枝可以跳过非必要的搜索。 回溯算法能解决如下问题: 组合问题:N个数里面按一定规则找出k个数的集合; 排列问题:N个数按一定规则全排列,有几种排列方式; 切割问题 阅读全文