classSolution: defisPrefixOfWord(self, sentence: str, searchWord: str) -> int: res = -1 for i, word in enumerate(sentence.split(' ')): if word.startswith(searchWord): res= i+1 break return res
classSolution: defmaxVowels(self, s: str, k: int) -> int: tmp = [0] * len(s) for i,x in enumerate(s): if x in ('a','e','i','o','u'): tmp[i] = 1 res= sum(tmp[:k]) t_sum = res for i in range(k,len(s)): t_sum -= tmp[i-k] t_sum += tmp[i] res = max(res, t_sum) return res
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right
from collections import Counter classSolution: def__init__(self): self.counter =Counter() self.res = 0 defjudge(self): flag = False for x,val in self.counter.items(): if val % 2 == 1: ifnot flag: flag=True else: return self.res += 1 defdfs(self,root): if root isNone: return self.counter[root.val] += 1 if root.left isNoneand root.right isNone: # 对叶节点进行判断 self.judge() self.counter[root.val] -= 1 return self.dfs(root.left) self.dfs(root.right) self.counter[root.val] -= 1 defpseudoPalindromicPaths(self, root: TreeNode) -> int: self.dfs(root) return self.res
classSolution: defmaxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: m = len(nums1) n= len(nums2) dp = [[0for _ in range(n)] for _ in range(m)] # print(dp) dp[0][0] = nums1[0] * nums2[0] res = dp[0][0] for i in range(1,m): dp[i][0] = max(dp[i-1][0], nums1[i]*nums2[0]) res = max(res,dp[i][0]) for j in range(1,n): dp[0][j] = max(dp[0][j-1], nums1[0]*nums2[j]) res= max(res,dp[0][j]) for i in range(1,m): for j in range(1,n): dp[i][j] = max(max(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]) dp[i][j] = max(dp[i][j], dp[i-1][j-1]+nums1[i]*nums2[j]) dp[i][j] = max(dp[i][j], nums1[i]*nums2[j]) res = max(res,dp[i][j]) return res