Python


  • 0
    B
    class Solution(object):
        def findMaxLength(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if len(nums) < 1: return 0
            sum =  maxlen = 0 
            hashmap = {}
            for i, v in enumerate(nums):
                if v == 1:
                    sum += 1
                else:
                    sum -= 1
                if sum not in hashmap:
                    hashmap[sum] = i
                if sum == 0:
                    maxlen = i + 1
                elif sum in hashmap:
                    maxlen = max(maxlen, i-hashmap[sum])
            return maxlen
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.