第 80 场双周赛(python版)

 

 按照题意模拟就行

 1 class Solution:
 2     def strongPasswordCheckerII(self, password: str) -> bool:
 3         # T="!@#$%^&*()-+"
 4         if len(password)<8:
 5             # print(1)
 6             return False
 7         a=b=c=d=0
 8         for letter in password:
 9             # print(letter)
10             if letter.islower():
11                 a=1
12             if letter.isupper():
13                 b=1
14             if letter.isdigit():
15                 c=1
16             if (letter in "!@#$%^&*()-+"):
17                 d=1
18         # print("num")
19         # print(a,b,c,d)
20         if (a+b+c+d)!=4:
21             # print(2)
22             return False
23         for i in range(1,len(password)):
24             if password[i]==password[i-1]:
25                 # print(3)
26                 return False
27         return True

 

 

 二分答案

 1 class Solution:
 2     def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
 3         ls=len(spells)
 4         lp=len(potions)
 5         potions.sort()
 6         ans=[]
 7         for i,num in enumerate(spells):
 8             # print((i,num))
 9             l=0
10             r=lp-1
11             p=lp
12             while l<=r:
13                 mid=(l+r)//2
14                 if(num*potions[mid]>=success):
15                     p=mid
16                     r=mid-1
17                 else:
18                     l=mid+1
19             ans.append(lp-p)
20         return ans

 

 

大力出奇迹!

默认字典collections.defaultdict()的使用:

https://blog.csdn.net/weixin_44110891/article/details/89575020

 

 1 import collections
 2 from typing import *
 3 
 4 class Solution:
 5     def matchReplacement(self, s: str, sub: str, mappings: List[List[str]]) -> bool:
 6         dict=collections.defaultdict(set)
 7         for x in mappings:
 8             dict[x[0]].add(x[1])
 9         def check(s1,s2):#s1:原串 s2:比较串
10             for i in range(0,len(s1)):
11                 if(s1[i]!=s2[i]) and ((s2[i] not in dict )or (s1[i] not in dict[s2[i]])):
12                     return False
13             return True
14         for i in range(0,(len(s))):
15             # if (i + len(sub)-1 < len(s)):
16             #     print(s[i:i+len(sub)])
17             if (i+len(sub)-1<len(s)) and check(s[i:i+len(sub)],sub):
18                 return True
19         return False

 

 

 双指针+前缀和

 1 import collections
 2 from typing import *
 3 
 4 class Solution:
 5     def countSubarrays(self, nums: List[int], k: int) -> int:
 6         ans=0
 7         l=0
 8         r=0
 9         sum=0
10         while r<len(nums):
11             sum+=nums[r]
12             while (r-l+1)*sum>=k:
13                 sum-=nums[l]
14                 l+=1
15             ans+=r-l+1
16             r=r+1
17         return ans

 

posted @ 2022-06-12 22:07  pengge666  阅读(18)  评论(0编辑  收藏  举报