思路:首先就是0,1數量相同的字串,把0換成-1就是求和爲0的最長字串。
可能性:1、從一個開始求和,和爲0的最長字串
2、如果是中間字串爲的和爲0,也就是求和的過程中,有和相同的字串,下標相減,即爲中間的和爲0的字串長度。
def findMaxLength(nums):
"""
:type nums: List[int]
:rtype: int
"""
sums = [0] * len(nums)
dmap = {}
last = 0
res=0
for i in range(len(nums)):
last+= 2 * nums[i] - 1
sums[i] = last
if sums[i] in dmap and sums[i]!=0:
res =max(res,i+1-dmap[sums[i]])
else:
dmap[sums[i]]=i+1
if 0 in dmap:
res = max(res,dmap[0])
return res
print(findMaxLength([0,0,1,0,0,0,1,1]))