随笔分类 - leetcode
摘要:题目描述: 自己的提交: 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
阅读全文
摘要:题目描述: 方法一:暴力O(MN) class Solution: def findSubstring(self, s: str, words: List[str]) -> List[int]: res = [] if not s or not words: return res for j in
阅读全文
摘要:题目描述: 方法一:动态规划 class Solution: def f(self, n, m): if n < m: n, m = m, n if (n, m) in self.mem: return self.mem[(n, m)] if n == m: ans = 1 else: ans =
阅读全文
摘要:题目描述: 自己的提交:O(2**n∗n∗m),m 为字符串长度 class Solution: def maxLength(self, arr: List[str]) -> int: from collections import Counter if not arr:return 0 res =
阅读全文
摘要:题目描述: 参考格雷编码: class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: res = [] for i in range(2 ** n): res.append((i >> 1) ^ i
阅读全文
摘要:题目描述: class Solution: def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]: res = [] for i in range(1,1001): for j in r
阅读全文
摘要:题目描述: 方法:动态规划: 自底向上: class Solution: def minDistance(self, word1: str, word2: str) -> int: n = len(word1) m = len(word2) dp = [[0 for _ in range(n+1)]
阅读全文
摘要:题目描述: 方法一:动态规划 一:超时 class Solution: def numSquares(self, n: int) -> int: dp = [float("inf")] * (n + 1) dp[0] = 0 for i in range(1, n + 1): for j in ra
阅读全文
摘要:题目描述: 方法一:动态规划+使用柱状图的优化暴力方法 O(N*2M) O(NM) N为行数 class Solution: def maximalRectangle(self, matrix: List[List[str]]) -> int: maxarea = 0 dp = [[0] * len
阅读全文
摘要:题目描述: 方法一:O(N) O(N) class Solution: def largestRectangleArea(self, heights: List[int]) -> int: if not heights: return 0 n = len(heights) left_i = [0]
阅读全文
摘要:题目描述: 第一次提交: class Solution: def trap(self, height: List[int]) -> int: res = 0 def helper(height): nonlocal res if len(height) <= 1: return queue = []
阅读全文
摘要:题10: 回溯:另:动态规划复杂度更低 class Solution: def isMatch(self, s: str, p: str) -> bool: def helper(s,p): if not p: return not bool(s) if len(p)>=2 and p[1]=="*
阅读全文
摘要:题目描述: 第一次提交:超时 O(N**N) class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> List[str]: if not wordDict:return [] res = [] min_len,max_l
阅读全文
摘要:题77 回溯: class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res = [] def backtrack(i,temp_list): if len(temp_list)==k: res.append(te
阅读全文
摘要:题目描述: 方法: class Solution: def maximizeSweetness(self, A: List[int], K: int) -> int: def possible(x): k, temp = 0, 0 for a in A: temp += a if temp >= x
阅读全文
摘要:题目描述: 二维dp: class Solution: def probabilityOfHeads(self, prob: List[float], target: int) -> float: dp = [[0 for _ in range(target+1)] for _ in range(l
阅读全文
摘要:题目描述: 自己的提交: class Solution: def minAvailableDuration(self, slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]: res = [] i,
阅读全文
摘要:题目描述: 自己的提交: class Solution: def missingNumber(self, arr: List[int]) -> int: if len(arr) == 2: return arr[0] + (arr[1] - arr[0]) //2 if len(set(arr))
阅读全文
浙公网安备 33010602011771号