zoukankan      html  css  js  c++  java
  • 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

    Code
  • 相关阅读:
    Servlet和Filter的url匹配
    iterator的用法
    python学习笔记
    python的序列之列表
    java开发实战学习笔记3
    java学习笔记4
    Java Java集合
    Struts2中的几个符号
    DbHelper.cs
    做word,excel时需要引用com
  • 原文地址:https://www.cnblogs.com/graphics/p/1499768.html
Copyright © 2011-2022 走看看