题目
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.
解题思路
代码
class Solution:
def findMaxLength(self, nums: List[int]) -> int:
res = ct = 0
dic = {0: 0}
for i, n in enumerate(nums, 1):
ct += 1 if n else -1
if ct in dic:
res = max(res, i - dic[ct])
else:
dic[ct] = i
return res