525. 连续数组
为保证权益,题目请参考 525. 连续数组(From LeetCode).
解决方案1
Python
python
from typing import List
class Solution:
def findMaxLength(self, nums: List[int]) -> int:
sum = 0
bef = {0: -1}
ans = 0
for index, num in enumerate(nums):
sum += 1 if num == 1 else -1
if sum in bef:
ans = max(ans, index - bef.get(sum))
else:
bef[sum] = index
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16