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
  • 相关阅读:
    js动态生成表格
    My97DatePicker显示时间控件的使用方法
    理解Action,Service和Dao功能(转)
    Myeclipseforspring 10破解
    MySQL常用命令(参考资料,部分改动)
    Struts2---Result(传统Web应用程序与Ajax应用程序的异同)
    正则表达式笔记
    day5.字符串内置函数
    day5.数据类型简介
    day4.变量+程序交互
  • 原文地址:https://www.cnblogs.com/graphics/p/1499768.html
Copyright © 2011-2022 走看看