Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.
Notice
There is at least one subarray that it's sum equals to zero.
Example
Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].
Basic Idea:
利用前缀和的性质,sums[i]存放了前i个数的和。则第 a ~ b 个数字的和就是 sums[b] - sums[a - 1]。所以要找连续和为0的段,我们只要找每个数字之前有无 sums[i] == sums[this index] 即可。
Java Code:
publicclassSolution{/** * @paramnums: A list of integers * @return: A list of integers includes the index of the first number * and the index of the last number*/publicArrayList<Integer>subarraySum(int[]nums){Map<Integer,Integer> sums =newHashMap<>();ArrayList<Integer> ret =newArrayList<>();sums.put(0,0);int sum =0;for(int i =0; i <nums.length;++i){ sum += nums[i];if(sums.containsKey(sum)){ret.add(sums.get(sum));ret.add(i);return ret;}sums.put(sum, i +1);}returnnewArrayList<Integer>();}}
class Solution:
"""
@param: nums: A list of integers
@return: A list of integers includes the index of the first number and the index of the last number
"""
def subarraySum(self, nums):
preSum = {}
preSum[0] = 0
currSum = 0
for i in range(len(nums)):
currSum += nums[i]
if currSum in preSum:
return [preSum[currSum], i]
preSum[currSum] = i + 1
return []