这周赛出问题了,第三题是5334。推文数首先,在线测试出现了500的错误。后面提交的时候遇到一些答错的情况,系统会返回一个错误的输入,这样就很难找到错误的地方,也很迷惑,但其实代码应该是可以通过的,我是在比赛之后通过的。5332.检查一个整数及其双精度值是否存在。第一次用set,结果发现不能处理0的情况,所以改用collections.Counter,因为0的个数需要统计为0,虽然0的2次还是0本身,但只有一个0仍然不够。代码类解决方案:defcheckIfExist(self,arr:List[int])->bool:importcollectionss=collections.Counter(arr)fornins:ifn==0:ifs[n]>1:returnTrueelifn<<1ins:returnTrue返回False5333。做字谜的最少步数https://leetcode-cn.com/conte...https://leetcode-cn.com/probl...解题思路用两个Counter统计出现的字母个数分别在s和t中。只看t中比s多的字母,一共多多少个,加起来就是答案码类解法:defminSteps(self,s:str,t:str)->int:importcollectionsc1=collections.Counter(s)c2=collections.Counter(t)ans=0forcinc2:ifcnotinc1:ans+=c2[c]else:ifc2[c]>c1[c]:ans+=c2[c]-c1[c]返回ans1348。推文数https://leetcode-cn.com/probl...https://leetcode-cn.com/conte...解题思维竞赛的时候样本返回有问题。本身不难,维护一个字典,把每个用户的时间放到一个数组中,先排序,然后按两点找到起点,向后遍历。需要注意的是,如果后面的时间范围内没有推文,也要在数组中加入相应个数的0。代码importbisectclassTweetCounts:def__init__(self):self.ul={}defrecordTweet(self,tweetName:str,time:int)->None:iftweetNamenotinself.ul:self.ul[tweetName]=[]self.ul[tweetName].append(time)defgetTweetCountsPerFrequency(self,freq:str,tweetName:str,startTime:int,endTime:int)->列表[int]:self.ul[tweetName].sort()#print(self.ul[tweetName])iffreq=='minute':f=60eliffreq=='hour':f=3600else:f=86400b=bisect.bisect(self.ul[tweetName],startTime-1)ans=[]cnt=0limit=startTime+fforninself.ul[tweetName][b:]:#print(n)ifn>endTime:ans.append(cnt)r=(endTime-limit)//fiflimit
