#930
Binary Subarrays With Sum
pupil · 580 · lc medium +29 · verified · 68.3% accepted · 4,802 likes · top 75%
Description
You are given a binary array nums (elements are 0 or 1) and an integer goal. Return the number of non-empty contiguous subarrays whose elements sum to exactly goal.
Example 1:
Input: nums = [1,0,1,0,1], goal = 2
Output: 4
Explanation: The 4 subarrays are bolded and underlined below:
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
Example 2:
Input: nums = [0,0,0,0,0], goal = 0
Output: 15
Code
1
2
3