摘要:
套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
人间烟火地三鲜
阅读(445)
评论(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
人间烟火地三鲜
阅读(218)
评论(0)
推荐(1)
摘要:
代码思路:用三个set标记当前位置所在的行、主、次对角线方向上是否合法。 class Solution(object): def solveNQueens(self, n): """ :type n: int :rtype: List[List[str]] """ # 返回值 res = [] # 阅读全文
posted @ 2020-11-23 16:57
人间烟火地三鲜
阅读(271)
评论(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
人间烟火地三鲜
阅读(200)
评论(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
人间烟火地三鲜
阅读(238)
评论(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
人间烟火地三鲜
阅读(206)
评论(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
人间烟火地三鲜
阅读(221)
评论(0)
推荐(0)
摘要:
还是套回溯模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class CombinationIterator(object): def __init__(self, characters, combinationLength): 阅读全文
posted @ 2020-11-23 16:44
人间烟火地三鲜
阅读(183)
评论(0)
推荐(1)
摘要:
关于回溯,可参考: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
人间烟火地三鲜
阅读(165)
评论(0)
推荐(0)
摘要:
还是套模板,模板可参考: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
人间烟火地三鲜
阅读(172)
评论(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
人间烟火地三鲜
阅读(127)
评论(0)
推荐(0)
摘要:
原串里有数字、也有字母,所以每选一个字符要加以判断,要是字母则有两种选择,数字就一种。 还是套模板,模板可参考:https://www.cnblogs.com/panweiwei/p/14025143.html class Solution(object): def __init__(self): 阅读全文
posted @ 2020-11-23 16:36
人间烟火地三鲜
阅读(245)
评论(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
人间烟火地三鲜
阅读(84)
评论(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
人间烟火地三鲜
阅读(105)
评论(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
人间烟火地三鲜
阅读(78)
评论(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
人间烟火地三鲜
阅读(101)
评论(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
人间烟火地三鲜
阅读(72)
评论(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
人间烟火地三鲜
阅读(98)
评论(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
人间烟火地三鲜
阅读(313)
评论(0)
推荐(0)
摘要:
参考:https://zhuanlan.zhihu.com/p/302415065 关于回溯 回溯是递归的“副产品”,并非高效算法,通过剪枝可以跳过非必要的搜索。 回溯算法能解决如下问题: 组合问题:N个数里面按一定规则找出k个数的集合; 排列问题:N个数按一定规则全排列,有几种排列方式; 切割问题 阅读全文
posted @ 2020-11-23 16:19
人间烟火地三鲜
阅读(426)
评论(0)
推荐(1)
摘要:
class MagicDictionary(object): def __init__(self): """ Initialize your data structure here. """ self.mydict = {} def buildDict(self, dictionary): """ 阅读全文
posted @ 2020-11-23 16:09
人间烟火地三鲜
阅读(107)
评论(0)
推荐(0)
摘要:
class Solution(object): def displayTable(self, orders): """ :type orders: List[List[str]] :rtype: List[List[str]] """ # 返回值 ans = [] # 第一趟遍历,listFood统 阅读全文
posted @ 2020-11-23 16:08
人间烟火地三鲜
阅读(126)
评论(0)
推荐(1)
摘要:
class Solution(object): def numRabbits(self, answers): """ :type answers: List[int] :rtype: int """ if not answers: return 0 mydict = {} for item in a 阅读全文
posted @ 2020-11-23 16:06
人间烟火地三鲜
阅读(135)
评论(0)
推荐(0)
摘要:
思路:将单词拆分出来的字符作为dict的key即可。 注意:python中key不能是list,需要转成tuple类型。 class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: 阅读全文
posted @ 2020-11-23 16:04
人间烟火地三鲜
阅读(96)
评论(0)
推荐(0)
摘要:
class Solution(object): def topKFrequent(self, words, k): """ :type words: List[str] :type k: int :rtype: List[str] """ # 用字典统计每个单词的数量 mydict = {} for 阅读全文
posted @ 2020-11-23 16:02
人间烟火地三鲜
阅读(85)
评论(0)
推荐(0)
摘要:
class FindElements(object): def getFolderNames(self, names): """ :type names: List[str] :rtype: List[str] """ mydict = {} res = [] for item in names: 阅读全文
posted @ 2020-11-23 16:01
人间烟火地三鲜
阅读(158)
评论(0)
推荐(0)
摘要:
class FindElements(object): # treeNode.left.val == 2 * x + 1 # treeNode.right.val == 2 * x + 2 def __init__(self, root): """ :type root: TreeNode """ 阅读全文
posted @ 2020-11-23 15:59
人间烟火地三鲜
阅读(99)
评论(0)
推荐(0)
摘要:
class Solution(object): def __init__(self): self.mydict = {} # 1、dfs求每个节点的子树和,并用字典统计 # 2、遍历字典,按values域的大小降序排:mylist = sorted(mydict.items(), key = lam 阅读全文
posted @ 2020-11-23 15:56
人间烟火地三鲜
阅读(129)
评论(0)
推荐(0)
摘要:
class Solution(object): def largestValsFromLabels(self, values, labels, num_wanted, use_limit): """ :type values: List[int] :type labels: List[int] :t 阅读全文
posted @ 2020-11-23 15:54
人间烟火地三鲜
阅读(116)
评论(0)
推荐(0)
摘要:
代码思路:横纵坐标的平方和,放到新list中,然后将新list升序排,取前K个即可。 class Solution(object): def kClosest(self, points, K): """ :type points: List[List[int]] :type K: int :rtyp 阅读全文
posted @ 2020-11-23 15:52
人间烟火地三鲜
阅读(127)
评论(0)
推荐(0)
摘要:
代码思路:用文件内容作key域,value域需要拼接,是文件全名。 import re class Solution(object): def findDuplicate(self, paths): """ :type paths: List[str] :rtype: List[List[str]] 阅读全文
posted @ 2020-11-23 15:49
人间烟火地三鲜
阅读(153)
评论(0)
推荐(0)
摘要:
class Solution(object): def frequencySort(self, s): """ :type s: str :rtype: str """ mydict = {} for item in s: if item in mydict.keys(): mydict[item] 阅读全文
posted @ 2020-11-23 15:47
人间烟火地三鲜
阅读(116)
评论(0)
推荐(0)
摘要:
注:sorted()可以排字符串。 class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] """ mydict = {} for item in 阅读全文
posted @ 2020-11-23 15:45
人间烟火地三鲜
阅读(125)
评论(0)
推荐(0)
摘要:
代码一:快慢指针 class Solution(object): # 快慢指针 def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ slow, fast = head, head finder = he 阅读全文
posted @ 2020-11-23 15:42
人间烟火地三鲜
阅读(84)
评论(0)
推荐(0)
摘要:
代码一:双指针 class Solution(object): # 双指针 def pairSums(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[List[int]] """ if not 阅读全文
posted @ 2020-11-23 15:38
人间烟火地三鲜
阅读(97)
评论(0)
推荐(0)
摘要:
class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ words = s.split() return ' '.join(words[::-1]) if __name__ == '__m 阅读全文
posted @ 2020-11-23 15:35
人间烟火地三鲜
阅读(88)
评论(0)
推荐(0)
摘要:
class Solution(object): def uniqueOccurrences(self, arr): """ :type arr: List[int] :rtype: bool """ mydict = {} for item in arr: if item in mydict.key 阅读全文
posted @ 2020-11-23 15:32
人间烟火地三鲜
阅读(100)
评论(0)
推荐(0)
摘要:
代码一思路: 设两个指针cur、i,cur是有效元素下标,初值为0,i是从第一个元素开始的遍历指针;另外用count记录当前相同元素的个数; i和cur指向的元素相同且count值小于2,说明cur指向的元素出现第二次,为有效元素,cur、i右移,count计数器加1; i和cur指向的元素相同且c 阅读全文
posted @ 2020-11-23 15:30
人间烟火地三鲜
阅读(105)
评论(0)
推荐(0)
摘要:
代码一:用字典。 class Solution(object): def smallerNumbersThanCurrent(self, nums): """ :type nums: List[int] :rtype: List[int] """ mydict = {} temp = [] for 阅读全文
posted @ 2020-11-23 15:25
人间烟火地三鲜
阅读(128)
评论(0)
推荐(0)
摘要:
class Solution(object): # 思路: # 从左往右遍历A,分别设置计数器记录上坡up和下坡down的长度; # 当不满足上坡了,就计数下坡; # 当下坡也不满足了,记录当前山脉的长度=up+down+1 # 遍历时若遇到两个元素相等,则跳过。 def longestMounta 阅读全文
posted @ 2020-11-23 15:22
人间烟火地三鲜
阅读(134)
评论(0)
推荐(0)

浙公网安备 33010602011771号