Leetcode 966 Solution
This article provides solution to leetcode question 966 (binary-subarrays-with-sum)
Access this page by simply typing in "lcs 966" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/binary-subarrays-with-sum
Solution
class Solution {
public:
int numSubarraysWithSum(vector<int>& A, int S) {
unordered_map<int, int> m;
m[0] = 1;
int cur = 0;
int ans = 0;
for (auto a: A)
{
cur += a;
ans += m[cur - S];
m[cur]++;
}
return ans;
}
};