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
  • 相关阅读:
    【工具】sublime使用技巧
    怎样存钱利息最大及怎样买房付款最省钱问题
    存钱问题
    玛丽莲问题
    用线程做一个火车票购票系统(可以根据需要选择线程个数)
    系统编程拷贝文件或者目录(可以做出一个动态库哦)
    mysql优化-数据库设计基本原则
    项目 数据可视化1
    读书笔记2-三体
    python数据学习3 布林带
  • 原文地址:https://www.cnblogs.com/graphics/p/1499768.html
Copyright © 2011-2022 走看看