11 2019 档案
摘要:分治法 描述: 1、分:将问题柴分为几个子问题,这些子问题和原问题相似只是量级上小一些。 2、治:递归地解决每一个子问题,然后结合这些子问题的解决方案构造出原问题的解决方案。 例:二分搜索、快速排序、归并排序 习题 一、快速指数 题:计算 an def fast_power(x, n): if n
阅读全文
摘要:当给定一个数组,要想到一些点: 1、是否已排序 2、是否有重复数字 3、是否有负数 一:常规二分搜索 def bi_search_iter(alist, item): left, right = 0, len(alist) - 1 while left <= right: mid = (left +
阅读全文
摘要:题目描述: 自己的提交: class Solution: def numWays(self, steps: int, arrLen: int) -> int: l = min(steps,arrLen) dp = [0] * l dp[0] = 1 MOD = 10 ** 9 + 7 for ste
阅读全文
摘要:题目描述: 自己的提交: class Solution: def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]: products.sort() res = [] for i in r
阅读全文
摘要:题目描述: 自己的提交: class Solution: def countServers(self, grid: List[List[int]]) -> int: from collections import Counter m,n = len(grid),len(grid[0]) falg =
阅读全文
摘要:题目描述: 自己的解: class Solution: def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int: res = 0 n = len(points) if n < 2: return res pre = poin
阅读全文
摘要:搜索 一、顺序查找 def search(num_list, val): # If empty if num_list == None: return -1 for i in range(0, len(num_list)): if (num_list[i] == val): return i ret
阅读全文
摘要:题目描述:编码 方法一: class Solution(object): def encode(self, n): if n == 0: return "" n -= 1 A = ['0' if n % 2 == 0 else '1'] # 01 2345 e = 2 while n >= e: n
阅读全文
摘要:题目描述: 方法: class Solution(object): def findSmallestRegion(self, regions, region1, region2): parent = {} for row in regions: top = row.pop(0) for bot in
阅读全文
摘要:题目描述: 自己的提交: class Solution: def minPushBox(self, grid: List[List[str]]) -> int: driction = [(0,1),(0,-1),(-1,0),(1,0)] for i in range(len(grid)): for
阅读全文
摘要:题目描述: 方法一:动态规划 O(N) class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0, -1,-1] for n in nums: tmp = dp[:] for i in range(3): if
阅读全文
摘要:题目描述: 方法一: class FindElements: def __init__(self, root: TreeNode): self.d = set() def f(r, x): if r: self.d.add(x) r.val = x f(r.left, 2 * x + 1) f(r.
阅读全文
摘要:题目描述: 自己的提交: class Solution: def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]: m,n = len(grid),len(grid[0]) while k: tmp = [[0] *
阅读全文
摘要:题目描述: 方法:穷举暴力 class Solution: def maxScoreWords(self, words: List[str], letters: List[str], score: List[int]) -> int: from collections import Counter
阅读全文
摘要:题目描述: 自己的提交: class Solution: def closedIsland(self, grid: List[List[int]]) -> int: def dfs(grid,r,c): nr = len(grid) nc = len(grid[0]) if r<0 or c<0 o
阅读全文
摘要:题目描述: 自己的提交: class Solution: def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) -> List[List[int]]: res = [[],[]] for v,i in enume
阅读全文
摘要:题目描述: 自己的提交: class Solution: def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int: nums = [[0] * m for _ in range(n)] for i,j in indice
阅读全文
摘要:题目描述: 方法:区间dp O(N^3) class Solution: def minimumMoves(self, A: List[int]) -> int: N = len(A) dp = [[0] * (N+1) for _ in range(N+1)] for i in range(N+1
阅读全文
摘要:题目描述: 方法一:深度优先: class Solution: def treeDiameter(self, edges: List[List[int]]) -> int: adjacency = collections.defaultdict(set) for i,j in edges: adja
阅读全文
摘要:题目描述: 自己的提交: class Solution: def transformArray(self, arr: List[int]) -> List[int]: if len(arr) < 3: return arr flag = True while flag: tmp = [] tmp.a
阅读全文
摘要:题目描述: class Leaderboard: def __init__(self): self.map = collections.Counter() def addScore(self, playerId: int, score: int) -> None: self.map[playerId
阅读全文
摘要:题目描述: 唯一的结论是如果数组中所有数的最大公约数为 1,则存在解,否则不存在。所以只需要计算所有数最大公约数即可,时间复杂度O(nlog(m)),其中 m 为数字大小。 class Solution: def isGoodArray(self, nums: List[int]) -> bool:
阅读全文
摘要:题目描述: 自己的提交: class Solution: def minimumSwap(self, s1: str, s2: str) -> int: count = {"xy":0,"yx":0} for i in range(len(s1)): if s1[i] == s2[i]: conti
阅读全文
摘要:题目描述: 自己的提交:超时: class Solution: def numberOfSubarrays(self, nums, k: int) -> int: dp = [0]* (len(nums)+1) res = 0 for i in range(len(nums)): if nums[i
阅读全文
摘要:题目描述: 自己的提交:O(N) class Solution: def minRemoveToMakeValid(self, s: str) -> str: #from collections import Counter flag = [True] * len(s) stack = [] for
阅读全文
浙公网安备 33010602011771号