随笔分类 - leetcode
摘要:题目描述: 方法一:二叉搜索树+滑动窗口 方法二:桶排序 O(N) class Solution: def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool: from collections i
阅读全文
摘要:题目描述: 第一次提交:超时 class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: for i in range(len(nums)-1): for j in range(i+1,min
阅读全文
摘要:题目描述: 第一次提交: class Solution: def containsDuplicate(self, nums: List[int]) -> bool: nums2 = set(nums) return not len(nums2)==len(nums) 方法二:哈希表 class So
阅读全文
摘要:题目描述: 方法一:回溯 class Solution: def combinationSum3(self, k: int, n: int) -> List[List[int]]: res = [] def helper(k,n,start,tmp): if k==0: if n==0: res.a
阅读全文
摘要:题目描述: 方法一:堆排序* class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: def adjust_heap(idx, max_len): left = 2 * idx + 1 right = 2 *
阅读全文
摘要:题目描述: 第一次提交: class Solution: def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]: indegree = [0 for _ in range(numCourse
阅读全文
摘要:题目描述: 方法一:双指针 O(N) class Solution: def minSubArrayLen(self, s: int, nums: List[int]) -> int: left = 0 res = float('inf') sum = 0 for i in range(len(nu
阅读全文
摘要:题目描述: 方法一: class Solution: def findMaximumXOR(self, nums: List[int]) -> int: root = TreeNode(-1) for num in nums: cur_node = root #当前的node for i in ra
阅读全文
摘要:题目描述: 第一次提交:(超出时间限制) class Solution: def findWords(self, board: List[List[str]], words: List[str]) -> List[str]: def dfs(word,i,j,visited): if len(wor
阅读全文
摘要:题目描述: 方法一: class WordDictionary: def __init__(self): """ Initialize your data structure here. """ #from collections import defaultdict self.lookup = {
阅读全文
摘要:题目描述: 方法一: class Trie: def __init__(self): """ Initialize your data structure here. """ self.tree = {} def insert(self, word: str) -> None: """ Insert
阅读全文
摘要:题目描述: 方法一:dfs class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: def dfs(i, adjacency, flags): if flags[i]
阅读全文
摘要:题目描述: 第一次提交: class Solution: def isIsomorphic(self, s: str, t: str) -> bool: dicA = {} dicB = {} for i in range(len(s)): if s[i] not in dicA: dicA[s[i
阅读全文
摘要:方法一: class Solution(object): def minimumSemesters(self, N, relations): """ :type N: int :type relations: List[List[int]] :rtype: int """ dic = {} dic2
阅读全文
摘要:方法一: class Solution: def minimumCost(self, N: int, conections: List[List[int]]) -> int: def find(i): if father[i]==i: return i else: father[i]=find(fa
阅读全文
摘要:第一次提交: class Solution: def isArmstrong(self, N: int) -> bool: n = N l = len(str(N)) res = 0 while N: a = N % 10 res += a**l N = N//10 if res == n: ret
阅读全文
摘要:第一次提交: class Solution: def largestUniqueNumber(self, A: List[int]) -> int: dict = {} for i in A: if i not in dict: dict[i] = 1 else: dict[i] += 1 res
阅读全文
摘要:题目描述: 方法一:dfs class Solution: def numIslands(self, grid: List[List[str]]) -> int: def dfs(grid,r,c): nr = len(grid) nc = len(grid[0]) if r<0 or c<0 or
阅读全文
摘要:题目描述: class Solution: def maxAbsValExpr(self, arr1, arr2) -> int: def function(s1,s2): result1=[] result2=[] result3=[] result4=[] for i in range(len(
阅读全文
摘要:题目描述: class Solution(object): def mctFromLeafValues(self, arr): """ :type arr: List[int] :rtype: int """ n = len(arr) f = {1: [0] * n} for l in range(
阅读全文
浙公网安备 33010602011771号