随笔分类 - leetcode
摘要:自己的提交: class Solution: def toHexspeak(self, num: str) -> str: num = hex(int(num)) num = str(num)[2:] res = "" for i in num: if i == "0": i = "O" if i
阅读全文
摘要:题目描述: 自己的提交: 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
阅读全文
摘要:题目描述:编码 方法一: 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:
阅读全文
浙公网安备 33010602011771号