Skip to content

Commit 5746c33

Browse files
authored
Update 930-binary-subarrays-with-sum.js
1 parent d098465 commit 5746c33

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

930-binary-subarrays-with-sum.js

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} goal
4+
* @return {number}
5+
*/
6+
var numSubarraysWithSum = function(nums, goal) {
7+
const n = nums.length
8+
const hash = { 0: 1 }
9+
let res = 0
10+
let sum = 0
11+
for(let i = 0; i < n; i++) {
12+
const e = nums[i]
13+
sum += e
14+
const diff = sum - goal
15+
if(hash[diff] != null) res += hash[diff]
16+
if(hash[sum] == null) hash[sum] = 1
17+
else hash[sum]++
18+
}
19+
20+
return res
21+
};
22+
23+
// another
24+
125
/**
226
* @param {number[]} A
327
* @param {number} S

0 commit comments

Comments
 (0)