Skip to content

Commit b764e7f

Browse files
authored
Create 930. Binary Subarrays With Sum (#428)
2 parents f3c7bfb + 88833cb commit b764e7f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

930. Binary Subarrays With Sum

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
3+
public:
4+
5+
int numSubarraysWithSum(vector<int>& nums, int goal) {
6+
7+
unordered_map<int,int> mpp;
8+
9+
int sum = 0;
10+
int cnt = 0;
11+
12+
for(auto i:nums)
13+
{
14+
sum+=i;
15+
if(sum==goal) cnt++;
16+
if(mpp.find(sum-goal)!=mpp.end())
17+
18+
{
19+
20+
cnt+=mpp[sum-goal];
21+
22+
}
23+
mpp[sum]++;
24+
25+
}
26+
27+
return cnt;
28+
29+
}
30+
31+
};

0 commit comments

Comments
 (0)