Data Structure Array: Find if there is a subarray with 0 sum

http://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <stack>
 6 #include <string>
 7 #include <fstream>
 8 #include <map>
 9 #include <set>
10 using namespace std;
11 
12 bool zerosubarray(int arr[], int n) {
13     set<int> S;
14     int sum = 0;
15     for (int i = 0; i < n; i++) {
16         sum += arr[i];
17         if (arr[i] == 0 || sum == 0 || S.find(sum) != S.end()) return true;
18         S.insert(sum);
19     }
20     return false;
21 }
22 
23 int main() {
24     int arr[5] = {4, 2, -3, 1, 6};
25     if (zerosubarray(arr, 5)) cout << "yes" << endl;
26     else cout << "no" << endl;
27     return 0;
28 }
原文地址:https://www.cnblogs.com/yingzhongwen/p/3655600.html