Binary Subarrays With Sum
Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum goal.
A subarray is a contiguous part of the array.
Example 1
Input
nums = [1,0,1,0,1], goal = 2Output
4There are 4 contiguous non-empty subarrays whose elements sum to 2.
Example 2
Input
nums = [0,0,0,0,0], goal = 0Output
15Every non-empty subarray of all zeros has sum 0, giving 15 total subarrays.
Constraints
- 1 <= nums.length <= 3 * 10^4
- nums[i] is either 0 or 1.
- 0 <= goal <= nums.length