• 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
  • 相关阅读:
    LeetCode 461. Hamming Distance
    LeetCode 442. Find All Duplicates in an Array
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode Find the Difference
    LeetCode 415. Add Strings
    LeetCode 445. Add Two Numbers II
    LeetCode 438. Find All Anagrams in a String
    LeetCode 463. Island Perimeter
    LeetCode 362. Design Hit Counter
    LeetCode 359. Logger Rate Limiter
  • 原文地址:https://www.cnblogs.com/graphics/p/1499768.html
Copyright © 2020-2023  润新知