随笔分类 - leetcode
摘要:题目描述: 方法一:二分 时间复杂度应该是O(n2) class Solution: def countSmaller(self, nums: List[int]) -> List[int]: num_length = len(nums) if not nums: return [] res = [
阅读全文
摘要:题目描述: 提交: class Solution: def reformatDate(self, date: str) -> str: l = date.split(" ") d = l[0][:-2] ml = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
阅读全文
摘要:题目描述: 方法一:动态规划 O(n) O(n) class Solution: def maxProfit(self, prices: List[int]) -> int: if len(prices) < 2: return 0 n = len(prices) sell = [0] * n bu
阅读全文
摘要:题目描述: 方法一:动态规划 O(n2) ->O(mn) m为字典中单词最大长度 class Solution: def respace(self, dictionary: List[str], sentence: str) -> int: d = {}.fromkeys(dictionary) n
阅读全文
摘要:题目描述: 方法一:纯volatile实现 class FizzBuzz { private int n; private volatile int f = 0; private volatile int b = 0; private volatile int fb = 0; private vol
阅读全文
摘要:题目描述: 提交:O(n) class Solution: def divingBoard(self, shorter: int, longer: int, k: int) -> List[int]: if k == 0: return [] res = [] for i in range(k,-1
阅读全文
摘要:题目描述: 方法一:动态规划 O(n) O(n) class Solution: def longestValidParentheses(self, s: str) -> int: n = len(s) if n==0:return 0 dp = [0]*n for i in range(len(s
阅读全文
摘要:题目描述: 方法一:动态规划 O(mn) O(mn) class Solution: def findLength(self, A: List[int], B: List[int]) -> int: n, m = len(A), len(B) dp = [[0] * (m + 1) for _ in
阅读全文
摘要:题目描述: 方法:动态规划+状态压缩 class Solution: def minNumberOfSemesters(self, n: int, dependencies: List[List[int]], k: int) -> int: dep = {} # 记录依赖于某节点的节点列表 for
阅读全文
摘要:题目描述: 方法一:原地哈希 class Solution: def firstMissingPositive(self, nums: List[int]) -> int: n = len(nums) for i in range(n): if nums[i] <= 0: nums[i] = n +
阅读全文
摘要:# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeDuplicateN
阅读全文
摘要:题目描述: 提交: class Solution: from typing import List def avoidFlood(self, rains: List[int]) -> List[int]: import heapq heap = [] res = [-1 if i != 0 else
阅读全文
摘要:题目描述: 第一次提交: 有错 未找出问题 留坑 class Solution: def getFolderNames(self, names: List[str]) -> List[str]: res = [] dic = collections.Counter() for name in nam
阅读全文
摘要:方法一: class Solution: def patternMatching(self, pattern: str, value: str) -> bool: count_a = sum(1 for ch in pattern if ch == 'a') count_b = len(patter
阅读全文
摘要:方法:递归 O(n) O(n) class Solution: def __init__(self): self.maxSum = float("-inf") def maxPathSum(self, root: TreeNode) -> int: def maxGain(node): if not
阅读全文
摘要:提交: class Solution: def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: count = collections.Counter(arr) order_dic = sorted(count.items
阅读全文
摘要:方法:倍增法dp ACM经典 class TreeAncestor: def __init__(self, n: int, parent: List[int]): self.cols = 20 # log(50000) < 20 self.dp = [[-1] * self.cols for _ i
阅读全文
摘要:方法:迭代+栈 O(N) class Solution { public TreeNode recoverFromPreorder(String S) { Deque<TreeNode> path = new LinkedList<>(); int pos = 0; while(pos < S.le
阅读全文
摘要:解:O(N) class Solution: def maxScoreSightseeingPair(self, A: List[int]) -> int: left, res = A[0], -1 for j in range(1, len(A)): res = max(res, left + A
阅读全文
摘要:题目描述: 提交:二分 O(nlogk) class Solution: def minDays(self, bloomDay: List[int], m: int, k: int) -> int: minday = min(bloomDay) maxday = max(bloomDay) def
阅读全文
浙公网安备 33010602011771号