forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
33 lines (29 loc) · 885 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
int numSubarraysWithSum(vector<int>& A, int S) {
vector<int> dp(A.size()+1,0);
map<int, vector<int> > mapp;
dp[0] = 0;
for(int i=0;i<A.size();i++){
dp[i+1] = dp[i] + A[i];
if(mapp.count(dp[i+1])>0)
mapp[dp[i+1]].push_back(i);
else
mapp[dp[i+1]] = vector<int>(1,i);
}
int num = 0;
for(int i=0;i<A.size();i++){
int del = S + dp[i];
if(mapp.count(del)>0){
for(int j=0;j<mapp[del].size();j++)
if(mapp[del][j] >= i)
num += 1;
}
// for(int j=i;j<A.size();j++){
// if(dp[j+1] - dp[i] == S)
// num += 1;
// }
}
return num;
}
};