Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d03438

Browse files
Create 0930-binary-subarrays-with-sum.java
1 parent 92fabae commit 4d03438

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int numSubarraysWithSum(int[] nums, int goal) {
3+
return helper(nums, goal) - helper(nums, goal - 1);
4+
}
5+
private int helper(int[] nums, int goal){
6+
if(goal < 0)
7+
return 0;
8+
9+
int res = 0, sum = 0;
10+
int l = 0;
11+
12+
for(int r = 0; r < nums.length; r++){
13+
sum += nums[r];
14+
while(sum > goal){
15+
sum -= nums[l];
16+
l += 1;
17+
}
18+
res += (r - l + 1);
19+
}
20+
return res;
21+
}
22+
}

0 commit comments

Comments
 (0)