Leetcode 190周赛:

第一题   5416. 检查单词是否为句中其他单词的前缀

难度简单

示例 1:

输入:sentence = "i love eating burger", searchWord = "burg"
输出:4
解释:"burg" 是 "burger" 的前缀,而 "burger" 是句子中第 4 个单词。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution:
    def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
        
        
        l1 = len(sentence)
        i=0
        j=0
        index = 1
        match=1
        while i<l1:
            if sentence[i]==' ':
                index+=1
                if match==1 and j>=len(searchWord):
                    break
                match = 1 
                j=0
                i+=1
            #print(sentence[i],searchWord[j],match)
            
            elif sentence[i]==searchWord[j] and match:
                j+=1
                i+=1
                match =1
                if j>=len(searchWord):
                    break
                    
                # print(sentence[i],searchWord[j],match)
            else:
                i+=1
                match =0
        
        if match ==0:
            return -1
        
        
        return index

 

第二题   5417. 定长子串中元音的最大数

给你字符串 s 和整数 k 。

请返回字符串 s 中长度为 k 的单个子字符串中可能包含的最大元音字母数。

英文中的 元音字母 为(a, e, i, o, u)。

示例 1:

输入:s = "abciiidef", k = 3
输出:3
解释:子字符串 "iii" 包含 3 个元音字母。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution:
    def maxVowels(self, s: str, k: int) -> int:
        l1 = len(s)
        clist=['a','e','i','o','u']
        t=0
        max1 = [0]*l1
        for i in range(k):
            if s[i] in ['a','e','i','o','u']:
                t+=1
        max1[k-1] =t
        for i in range(k,l1):
            t = max1[i-1]
            if s[i-k] in clist:
                t-=1
            if s[i] in clist:
                t+=1
            max1[i] = t
        return max(max1)

  

第三题:

给你一棵二叉树,每个节点的值为 1 到 9 。我们称二叉树中的一条路径是 「伪回文」的,当它满足:路径经过的所有节点值的排列中,存在一个回文序列。

请你返回从根到叶子节点的所有路径中 伪回文 路径的数目。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/pseudo-palindromic-paths-in-a-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# 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
class Solution:
    def pseudoPalindromicPaths (self, root: TreeNode) -> int:
        
        num =0 
        path=[]
        num = self.treepath(root,path,num)
        return num#-num//2
        
    def treepath(self,node,path,num):
        import copy
        if node.left is None and node.right is None:
            path1 = copy.copy(path)
            path1.append(node.val)
            oddnum=0
            rec={}
            # print(path1,node.val)
            for i in range(len(path1)):
                if path1[i] not in rec:
                    rec[path1[i]]=0
                rec[path1[i]]+=1
            for c in rec:
                if rec[c]%2==1:
                    oddnum+=1
                    if oddnum>1:
                        return num
            # print(path,num)
            return num+1
        # path.append(node.val)
        path1 = copy.copy(path)
        path1.append(node.val)
            if node.left is not None:
            num = self.treepath(node.left,path1,num)
        
      
        if node.right is not None:
            num = self.treepath(node.right,path1,num)
       
        return num

  

原文地址:https://www.cnblogs.com/SuckChen/p/12952924.html