连续未排序正负数相同最长子数组_python3
未排序数组中累加和为给定值的最长子数组系列问题补1
http://www.nowcoder.com/questionTerminal/545544c060804eceaed0bb84fcd992fb
同上一题,正数看作1,负数看作-1,k=0
m存储前i项和对应下标,a存储前i项和,ans最大长,即求第i项时m[a-k]是否存在
def solve(l, n): m = {0:-1} a = ans = 0 for i in range(n): if l[i] > 0: a += 1 elif l[i] < 0: a -= 1 if a not in m.keys(): m[a] = i else: ans = max(ans, i - m[a]) return ans while True: try: n = int(input()) l = list(map(int, input().split())) print(solve(l, n)) except EOFError: break