给定一个整数数组,检测是否存在一个和为零的子数组
问题:给定一个整数数组,写一个算法实现判断是否存在一个和为零的子数组。
答:算法思路:计算数组的前缀和,然后将前缀和进行排序,如果存在连续两个元素相同的情况即存在一个和为零的子数组,否则不存在。
算法的代码实现:
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys002
[Switching to process 17846 thread 0x0]
Zero Sum Sunarray Check Program
===============================
Given Array : 4 -1 2 1 -2 -1 5
YES, there is some range summing up to 0
Program ended with exit code: 0