Consecutive sequence with sum 0

简介:

You are given an integer array which contains positive integers, zero and negetive integers
count how many consecutive sequences in this array make a sum of 0.

exmaple
int[] a = {4, -1, 2, 1, -2, -1, 5, 0} ;
The result is 2
-1, 2, 1, -2, makes a sum of 0
2, 1, -2, -1, makes a sum of 0
0 makes a sum of 0

consider a consecutive sequence in array a, say xi, ... xj, if xi + ... + xj = 0, then x0 + .. + x(i-1) = x0 + .. + xj
so we can compute all the prefix sum of array a, and see whether there are equal elements among these sum
if there is, then there must be some consecutive sequence, makes sum of 0

 

ContractedBlock.gif Code

 

本文转自zdd博客园博客,原文链接:http://www.cnblogs.com/graphics/archive/2009/06/09/1499768.html,如需转载请自行联系原作者

相关文章
|
算法
LeetCode 128. Longest Consecutive Sequence
给定一个未排序的整数数组,找出最长连续序列的长度。 要求算法的时间复杂度为 O(n)。
64 0
LeetCode 128. Longest Consecutive Sequence
HDOJ 1005 Number Sequence
HDOJ 1005 Number Sequence
79 0
Maximum Subsequence Sum
最大连续子列和问题,在此给出题解 (浙大PTA https://pintia.cn/problem-sets/16/problems/665)
|
算法 C#
算法题丨Longest Consecutive Sequence
描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
1089 0
1104. Sum of Number Segments (20) consecutive subsequence 每个数出现的次数
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence.
938 0
|
人工智能 机器学习/深度学习
1007. Maximum Subsequence Sum (25)
简析:求最大子列和,并输出其首末元素。在线处理,关键在于求首末元素。 本题囧,16年9月做出来过,现在15分钟只能拿到22分,有一个测试点过不了。
948 0
|
人工智能
[LeetCode]--136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without
1196 0