Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. class Solution(object):
  2. def findMaxLength(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: int
  6. """
  7.  
  8. max_len = 0
  9. count_dict = {}
  10. count = 0
  11. index = 0
  12. for num in nums:
  13. if num == 0:
  14. count -= 1
  15. else:
  16. count += 1
  17.  
  18. if count == 0 and index + 1 > max_len:
  19. max_len = index + 1
  20.  
  21. old_index = count_dict.get(count)
  22.  
  23. if old_index is None:
  24. count_dict[count] = index
  25. else:
  26. new_len = index - old_index
  27. if max_len < new_len:
  28. max_len = new_len
  29.  
  30. index += 1
  31.  
  32. return max_len
  33.  
  34.  
  35. sol = Solution()
  36. print(sol.findMaxLength([0, 1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement