随笔分类 - leetcode
摘要:题目描述: 方法一:DFS枚举 O(s*t) O(max(ds,dt) d代表深度 class Solution: def isSubtree(self, s: TreeNode, t: TreeNode) -> bool: def isSameTree(s, t): if not s and no
阅读全文
摘要:题目描述: 方法一:O(W) O(W) W =365 class Solution: def mincostTickets(self, days: List[int], costs: List[int]) -> int: dayset = set(days) durations = [1,7,30]
阅读全文
摘要:题目描述: 提交: class Solution: def maxDiff(self, num: int) -> int: maxn, minn = num, num for i in range(10): s = str(num).replace(str(i), '9') maxn = max(i
阅读全文
摘要:题目描述: 提交: class Solution: def checkIfCanBreak(self, s1: str, s2: str) -> bool: a,b = sorted(list(s1)),sorted(list(s2)) n = len(a) if all(a[i] >= b[i]
阅读全文
摘要:题目描述: 方法一:记忆化递归+状态压缩 * from functools import lru_cache class Solution: def numberWays(self, hats: List[List[int]]) -> int: N = len(hats) M = 41 mod =
阅读全文
摘要:提交:O(N) class Solution: def destCity(self, paths: List[List[str]]) -> str: dic = {} for i,v in paths: dic[i] = v tmp = paths[0][1] while tmp in dic: t
阅读全文
摘要:题目描述: 提交:O(N) class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: c = k for i in nums: if i == 1: if c < k: return False else: c
阅读全文
摘要:题目描述: 方法一:暴力法O(m*max(n,klog(k)) O(k) 使用 vector<int> ans 记录各个行选一个数相加的和11. 记录的方法是,先保存第一行各数12. 然后把第二行的各数拿出来,组合相加13. 对其排列,超过 k 个就不需要保留了14. 相加之后记录回 ans ,下次
阅读全文
摘要:题目描述: 第一次提交: class Solution: def longestSubarray(self, nums, limit: int) -> int: res = 0 i,j = 0,0 n = len(nums) l = [[nums[0],0],[nums[0],0]] while i
阅读全文
摘要:题目描述: 方法:中序遍历 O(N) O(N) /* // Definition for a Node. class Node { public int val; public Node left; public Node right; public Node() {} public Node(in
阅读全文
摘要:方法:二分查找 def binary_search(mountain, target, l, r, key=lambda x: x): target = key(target) while l <= r: mid = (l + r) // 2 cur = key(mountain.get(mid))
阅读全文
摘要:题目描述: 方法一:分组异或 class Solution { public int[] singleNumbers(int[] nums) { int sum = 0; int [] res = new int[2]; for(int num:nums){ sum ^= num; } int lo
阅读全文
摘要:题目描述: 自己的提交: class Solution: def maxScore(self, s: str) -> int: d = {"0":0,"1":0} for i in s: d[i] += 1 res = 0 l,r = {"0":0,"1":0},d for v,i in enume
阅读全文
摘要:题目描述: 自己的提交:O(N) class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: copy1 = [0] + cardPoints[:] copy2 = [0] + cardPoints[::-1]
阅读全文
摘要:题目描述: 方法一: class Solution: def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]: a,s,m=[(0,0)],[nums[0][0]],len(nums) while a: b=[] for i,j
阅读全文
摘要:题目描述: 方法一:动态规划 O(n) class Solution: def constrainedSubsetSum(self, nums, k: int): dp = [0]*len(nums) dp[0] = nums[0] arr = [(nums[0],0)] for i in rang
阅读全文
摘要:题目描述: 提交: class Solution: def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]: def helper(num,node,temp): if node.left: helper(num+node.lef
阅读全文
摘要:题目描述: 方法一:递归 最坏O(N^2) class Solution: def verifyPostorder(self, postorder: List[int]) -> bool: def recur(i,j): if i >= j:return True p = i while posto
阅读全文
摘要:题目描述: 方法一:归并排序 O(nlogn) class Solution: def reversePairs(self, nums: List[int]) -> int: def mergeSort(l=0, r=len(nums)):#左闭右开区间 if r - l > 1: mid = (l
阅读全文
摘要:题一: 提交:层序遍历bfs O(N) O(N) class Solution: def levelOrder(self, root: TreeNode) -> List[int]: if not root : return [] queue = [root] res = [] while queu
阅读全文
浙公网安备 33010602011771号