azl397985856 / leetcode

LeetCode Solutions: A Record of My Problem Solving Journey.( leetcode题解,记录自己的leetcode解题之路。)

Home Page:https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/

Repository from Github https://github.comazl397985856/leetcodeRepository from Github https://github.comazl397985856/leetcode

二值化

azl397985856 opened this issue · comments

  1. [525] 连续数组

前缀和

class Solution:
    def findMaxLength(self, A: List[int]) -> int:
        A = [-1 if a == 0 else 1 for a in A]
        acc = 0
        ans = 0
        mapper = {0: -1}
        for i, a in enumerate(A):
            acc += a
            if acc not in mapper:
                mapper[acc] = i
            else:
                ans = max(ans, i - mapper[acc])

        return ans
commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.