Largest Continuous Sequence Zero Sum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| vector<int> Solution::lszero(vector<int> &A) { unordered_map<int,int> mp; mp[0] = -1; int start = -1, len = -1; for(int i = 0, sum = 0; i < A.size(); i++) { sum += A[i]; if(!mp.count(sum)) mp[sum] = i; else { int l = i - mp[sum]; if(len < l) { start = mp[sum]; len = l; } } } vector<int> res; for(int i = start + 1; i <= start + len; i++) res.push_back(A[i]); return res; }
|