做了四道题,最后两道很勉强,错了几次。刚刚看了第三题,虽然说是medium,但是我也不知道。我打开第四题,看到已经有人过了,就先做了第四题,然后回来做第一题。最后一步是纯暴力枚举,增加了几个条件:去除相邻的重复值,当函数输出为0时停止枚举,因为0按位与任何数仍然为0。因为在连续按位与的过程中,函数的output是单调递减的,所以如果差值大于当前最小差值,可以停止遍历,不过我觉得应该有更好的解决办法。第三题用的是模拟的方法,只有一个字母,两个字母依次..。String5464换酒问题https://leetcode-cn.com/conte...class解法:defnumWaterBottles(self,numBottles:int,numExchange:int)->int:c=numBottlescc=numBottleskp=numBottleswhilekp>=numExchange:cc=kp//numExchangekp=kp%numExchange+ccc+=ccreturnc5465子树中标签相同的节点个数https://leetcode-cn.com/conte...class解法:defcountSubTrees(self,n:int,edges:List[List[int]],labels:str)->List[int]:ed={}fors,einedges:ifsnotined:ed[s]=[]如果e不在ed中:ed[e]=[]ed[s].append(e)ed[e].append(s)ans=[0]*nvised=[False]*ndefvis(i):vised[i]=Truec={}c[labels[i]]=1ifiined:forchined[i]:ifnotvised[ch]:cc=vis(ch)forkincc:ifknotinc:c[k]=0c[k]+=cc[k]ans[i]=c[labels[i]]returncvis(0)returnans5466最多的不重叠加子字符串https://leetcode-cn.com/conte...类解决方案:defmaxNumOfSubstrings(self,s:str)->List[str]:d={}fori,chinenumerate(s):ifchnotind:d[ch]=[i,i,True]else:ifd[ch][1]!=i-1:d[ch][2]=Falsed[ch][1]=i#print(d)使用={}ans=[]nd=[]forchind:ifd[ch][2]:used[ch]=Trueans.append(s[d[ch][0]:d[ch][1]+1])对于d中的ch:如果不是d[ch][2]:cc=0ccx=set()f=Truemini=d[ch][0]maxi=d[ch][1]change=Truewhilechange:change=Falsei=d[ch][0]whilei<=maxi:ifs[i]inused:f=Falsebreakelse:ifmaxi
