描述
在由若干 0 和 1 组成的数组 A 中,有多少个和为 S 的非空子数组。
- A.length <= 30000
- 0 <= S <= A.length
- A[i] 为 0 或 1
样例1
Input: A = [1,0,1,0,1], S = 2
Output: 4
Explanation:
The 4 subarrays are bolded below:
[1,0,1]
[1,0,1]
[1,0,1,0]
[0,1,0,1]
样例2
Input: A = [0,0,0,0,0,0,1,0,0,0], S = 0
Output: 27
Explanation:
And 27 subarrays for S.
题解
前缀和:定义一个数组sumN+1,si表示数组A中前i个元素之和,然后遍历sum数组,计算si+S(含义:前i个元素之和是s