题目描述
给你一个二元数组 nums
,和一个整数 goal
,请你统计并返回有多少个和为 goal
的 非空 子数组。
子数组 是数组的一段连续部分。
示例 1:
输入:nums = [1,0,1,0,1], goal = 2
输出:4
解释:
有 4 个满足题目要求的子数组:[1,0,1]、[1,0,1,0]、[0,1,0,1]、[1,0,1]
示例 2:
输入:nums = [0,0,0,0,0], goal = 0
输出:15
提示:
1 <= nums.length <= 3 * 104
nums[i]
不是 0
就是 1
0 <= goal <= nums.length
解法
方法一:数组或哈希表 + 前缀和
我们可以用数组或哈希表 $cnt$ 记录每个前缀和出现的次数,其中 $cnt[i]$ 表示前缀和为 $i$ 的子数组个数。初始时 $cnt[0] = 1$。
接下来我们遍历数组 nums
,用变量 $s$ 维护当前的前缀和,对于每个 $s$,我们可以计算出 $s - goal$ 出现的次数,即为以当前位置结尾的满足条件的子数组个数,累加到答案中。然后我们将 $s$ 的计数值加 $1$。
最终的答案即为满足条件的子数组个数。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 nums
的长度。
| class Solution:
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
cnt = Counter({0: 1})
ans = s = 0
for v in nums:
s += v
ans += cnt[s - goal]
cnt[s] += 1
return ans
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | class Solution {
public int numSubarraysWithSum(int[] nums, int goal) {
int[] cnt = new int[nums.length + 1];
cnt[0] = 1;
int ans = 0, s = 0;
for (int v : nums) {
s += v;
if (s - goal >= 0) {
ans += cnt[s - goal];
}
++cnt[s];
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | class Solution {
public:
int numSubarraysWithSum(vector<int>& nums, int goal) {
int cnt[nums.size() + 1];
memset(cnt, 0, sizeof cnt);
cnt[0] = 1;
int ans = 0, s = 0;
for (int& v : nums) {
s += v;
if (s - goal >= 0) {
ans += cnt[s - goal];
}
++cnt[s];
}
return ans;
}
};
|
| func numSubarraysWithSum(nums []int, goal int) (ans int) {
cnt := map[int]int{0: 1}
s := 0
for _, v := range nums {
s += v
ans += cnt[s-goal]
cnt[s]++
}
return
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | /**
* @param {number[]} nums
* @param {number} goal
* @return {number}
*/
var numSubarraysWithSum = function (nums, goal) {
const cnt = new Array(nums.length + 1).fill(0);
cnt[0] = 1;
let ans = 0;
let s = 0;
for (const v of nums) {
s += v;
if (s >= goal) {
ans += cnt[s - goal];
}
++cnt[s];
}
return ans;
};
|
方法二:双指针
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | class Solution:
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
i1 = i2 = s1 = s2 = j = ans = 0
n = len(nums)
while j < n:
s1 += nums[j]
s2 += nums[j]
while i1 <= j and s1 > goal:
s1 -= nums[i1]
i1 += 1
while i2 <= j and s2 >= goal:
s2 -= nums[i2]
i2 += 1
ans += i2 - i1
j += 1
return ans
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | class Solution {
public int numSubarraysWithSum(int[] nums, int goal) {
int i1 = 0, i2 = 0, s1 = 0, s2 = 0, j = 0, ans = 0;
int n = nums.length;
while (j < n) {
s1 += nums[j];
s2 += nums[j];
while (i1 <= j && s1 > goal) {
s1 -= nums[i1++];
}
while (i2 <= j && s2 >= goal) {
s2 -= nums[i2++];
}
ans += i2 - i1;
++j;
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | class Solution {
public:
int numSubarraysWithSum(vector<int>& nums, int goal) {
int i1 = 0, i2 = 0, s1 = 0, s2 = 0, j = 0, ans = 0;
int n = nums.size();
while (j < n) {
s1 += nums[j];
s2 += nums[j];
while (i1 <= j && s1 > goal) s1 -= nums[i1++];
while (i2 <= j && s2 >= goal) s2 -= nums[i2++];
ans += i2 - i1;
++j;
}
return ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | func numSubarraysWithSum(nums []int, goal int) int {
i1, i2, s1, s2, j, ans, n := 0, 0, 0, 0, 0, 0, len(nums)
for j < n {
s1 += nums[j]
s2 += nums[j]
for i1 <= j && s1 > goal {
s1 -= nums[i1]
i1++
}
for i2 <= j && s2 >= goal {
s2 -= nums[i2]
i2++
}
ans += i2 - i1
j++
}
return ans
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 | /**
* @param {number[]} nums
* @param {number} goal
* @return {number}
*/
var numSubarraysWithSum = function (nums, goal) {
let i1 = 0,
i2 = 0,
s1 = 0,
s2 = 0,
j = 0,
ans = 0;
const n = nums.length;
while (j < n) {
s1 += nums[j];
s2 += nums[j];
while (i1 <= j && s1 > goal) s1 -= nums[i1++];
while (i2 <= j && s2 >= goal) s2 -= nums[i2++];
ans += i2 - i1;
++j;
}
return ans;
};
|