class Solution:
def findMaxLength(self, nums: List[int]) -> int:
prefix_sum = [0]
for num in nums:
if num==1:
prefix_sum.append(num+prefix_sum[-1])
else:
prefix_sum.append(prefix_sum[-1]-1)
res = 0
dic = {0:0}
for i in range(1,len(prefix_sum)):
if prefix_sum[i] in dic:
res = max(res,i-dic[prefix_sum[i]])
else:
dic[prefix_sum[i]] = i
return res
题目分析:和昨天是一摸一样的还是计算前缀和,然后利用hash表进行查找即可。不过这次的前缀和将0当作-1计算方便一点。