LeetCode 525 Contiguous Array (Python)

Posted by 小明MaxMing on April 26, 2020

题目

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

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅