Skip to content

Commit 833de84

Browse files
committed
Add 930-BinarySubarraysWithSum.cpp
1 parent dc515ed commit 833de84

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int numSubarraysWithSum(vector<int>& nums, int goal) {
4+
std::unordered_map<int, int> f;
5+
f[0] = 1;
6+
int cs(0), res(0);
7+
for(int p = 0; p < nums.size(); p++){
8+
cs += nums[p];
9+
int diff = cs - goal;
10+
if(f.count(diff)){res += f[diff];}
11+
++f[cs];
12+
}
13+
14+
return res;
15+
}
16+
}

0 commit comments

Comments
 (0)