05 2020 档案
摘要:四、子序列 1.最长子序列 给定一个字符串‘s’和一个整数k,找到其他字符串‘t’,使得‘t’是给定字 符串‘s’的最大子序列,同时‘t’的每个字符在字符串s中必须至少出现k次。 Input: s = "baaabaacba“, k = 3 Output : baaabaaba im
阅读全文
摘要:一、基于字符计数的问题 1.偶数子串的数量 给定一串0到9的数字。任务是计算在将整数转换为偶数时的子串的数量。 Input : str = "1234". Output : 6 “2”, “4”, “12”, “34”, “234”, “1234是6个子字符串,它们是偶数。 def
阅读全文
摘要:方法一: 二分法 O(nlogn) O(1) class Solution { public int findDuplicate(int[] nums) { int len = nums.length; int left = 1; int right = len - 1; while(left <
阅读全文
摘要:题目描述: 提交: class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n1,n2 = len(nums1),len(nums2) dp = [[0] * n2 for _ in ra
阅读全文
摘要:题目描述: 提交: class Solution: def pseudoPalindromicPaths (self, root: TreeNode) -> int: def helper(l,node): left,right = 0,0 if node.left == None and node
阅读全文
摘要:提交: class Solution: def maxVowels(self, s: str, k: int) -> int: n = len(s) dp = [0] * (n + 1) for i in range(n): if s[i] in "aeiou": dp[i + 1] = dp[i]
阅读全文
摘要:题目描述: 提交: class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: n = len(searchWord) sentence_l = sentence.split(" ") for i
阅读全文
摘要:题目描述: 方法一:动态规划 O(N) class Solution: def translateNum(self, num: int) -> int: str_num = str(num) n = len(str_num) dp = [1 for _ in range(n + 1)] for i
阅读全文
摘要:题目描述: 方法:前缀和+状态压缩 O(N) class Solution: def findTheLongestSubstring(self, s: str) -> int: bit_mask = eval('0b00000') state_first_idx = {eval('0b00000')
阅读全文
摘要:方法:自定义排序:O(logn) class Solution { public String minNumber(int[] nums) { String[] strs = new String[nums.length]; for(int i = 0; i < nums.length; i++)
阅读全文
摘要:提交:O(N) O(1) class Solution: def validPalindrome(self, s: str) -> bool: def helper(s): if s == s[::-1]: return True return False l,r = 0,len(s) - 1 wh
阅读全文
摘要:方法一: 来自讨论区 https://leetcode-cn.com/circle/discuss/Z2oiVE/ class Solution: def numPoints(self, p: List[List[int]], r: int) -> int: eps = 1e-8 def dist(
阅读全文
摘要:题目描述: 提交: class Solution: def peopleIndexes(self, f: List[List[str]]) -> List[int]: res = [] dic = collections.defaultdict(list) for i in range(len(f)
阅读全文
摘要:题目描述: 提交: class Solution: def arrangeWords(self, text: str) -> str: text = text.lower().split(" ") text.sort(key = lambda x:len(x)) text[0] = text[0][
阅读全文
摘要:提交: class Solution: def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int: count = 0 for i in range(len(startTime)):
阅读全文
摘要:题目描述: 方法一:找规律 class Solution { public int findNthDigit(int n) { int digit = 1; long start = 1; long count = 9; while(n > count){ n -= count; digit +=
阅读全文
摘要:题目描述: 提交:背包问题 class Solution: def largestNumber(self, cost, target: int) -> str: dp = [[0 for _ in range(target + 1)] for _ in range(10)] def tmax(a,b
阅读全文
摘要:题目描述: 提交: class Solution: def goodNodes(self, root: TreeNode) -> int: def helper(m,node): r,left,right = 0,0,0 if node.val >= m: r = 1 if node.left: l
阅读全文
摘要:题目描述: 提交: class Solution: def simplifiedFractions(self, n: int) -> List[str]: def gcd(a, b): if b > a: return gcd(b, a) if a % b == 0: return b return
阅读全文
摘要:题目描述: 方法一:栈 class Solution { public ListNode reverseKGroup(ListNode head, int k) { Deque<ListNode> stack = new ArrayDeque<>(); ListNode dummy = new Li
阅读全文
摘要:位操作 Bit Operation 一、按位操作是在单个字节的层面上对一个或多个位模式或二进制数字符号进行 的操作。处理器支持这种快速简单的操作,而且可以用来比较和计算数值。 l按位操作比除法快得多,是乘法的几倍速度,有时候也比加法快得多。 二、基本技巧 1.XOR myBits ^ 0 : 不
阅读全文
摘要:方法:前缀和 + 哈希表 O(N) class Solution { public int subarraySum(int[] nums, int k) { int count = 0,pre = 0; HashMap <Integer,Integer> mp = new HashMap<> ();
阅读全文
摘要:题目描述: 方法: class Solution { public int countDigitOne(int n) { int digit = 1,res = 0; int high = n / 10,cur = n % 10,low = 0; while(high != 0 || cur !=
阅读全文
摘要:题目描述: 方法一:快排 O(N) java class Solution { public int[] getLeastNumbers(int[] arr, int k) { if(k == 0|| arr.length == 0){ return new int[0]; } return qui
阅读全文
摘要:题目描述: 提交:O(N) class Solution: def buildArray(self, target: List[int], n: int) -> List[str]: res = [] a = "Push" b = "Pop" index = 1 for i in target: w
阅读全文
摘要:题目描述: 提交:O(N3) class Solution: def countTriplets(self, arr: List[int]) -> int: # count = 0 # for i in range(len(arr)): # for j in range(i+1,len(arr)):
阅读全文
摘要:题目描述: 解:动态规划 参考自: 作者:coldme-2链接:https://leetcode-cn.com/problems/number-of-ways-of-cutting-a-pizza/solution/dong-tai-gui-hua-by-coldme-2-2/来源:力扣(LeetC
阅读全文
摘要:题目描述: 方法一:dfs 从父节点到子节点 class Solution: def minTime(self, n: int, edges: List[List[int]], hasApple: List[bool]) -> int: maps = collections.defaultdict(
阅读全文
摘要:题目描述: 方法一: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
阅读全文
摘要:一、0/1 背包 • 你背着一个背包闯入一家珠宝店,店里有林林总总的格式珠宝,各不重样。每一个珠 宝都有重量和价值。但是你的书包有承载上限。 • 想成为江湖老大,你需要找到一种装包方法使得包内物品的重量不超过其限定值且使 包内物品的价值最大 def knapSack(W, wt, val, n):
阅读全文
摘要:一:买卖股票 I • 给定一个数组,表示每天的股票价格。 • 你可以进行一次交易(先买再卖),问如何能得到最大利润。 def maxProfit(prices): if len(prices) < 2: return 0 min_price = prices[0] max_profit = 0 fo
阅读全文
摘要:题目描述: 方法一: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]
阅读全文
摘要:动态规划 Dynamic Programming • 拆分(Divide): 将一个复杂问题拆分成一系列的简单子问题,每一次解决一个子 问题并将其结果存储起来。理想情况下用基于内存的数据结构。 • 查找(lookup):在下一次遇到相同的子问题的时候,直接查找之前计算过的结果 而不是重新计算。理想情
阅读全文
摘要:题目描述: 提交: 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
阅读全文
浙公网安备 33010602011771号