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
  • 相关阅读:
    iOS 定制controller过渡动画 ViewController Custom Transition使用体会
    iOS 和 Android 中的Alert
    iOS 中的frame,bounds,center,transform关联
    Android Services重点记录
    iOS 中的字体预览
    iOS 中关于ViewController总结
    iOS7 和 iOS6的页面兼容问题
    docker安装并修改Nginx镜像
    docker环境搭建
    Eclipse中.setting目录下文件介绍
  • 原文地址:https://www.cnblogs.com/graphics/p/1499768.html
Copyright © 2011-2022 走看看