Skip to content

Commit 9775bb4

Browse files
committed
feat: add solutions to lc problem: No.0930
No.0930.Binary Subarrays With Sum
1 parent b102005 commit 9775bb4

File tree

2 files changed

+119
-69
lines changed

2 files changed

+119
-69
lines changed

Diff for: solution/0900-0999/0930.Binary Subarrays With Sum/README.md

+65-33
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45-
前缀和 / 双指针。
45+
**方法一:数组或哈希表 + 前缀和**
46+
47+
我们可以用数组或哈希表 $cnt$ 记录每个前缀和出现的次数,其中 $cnt[i]$ 表示前缀和为 $i$ 的子数组个数。初始时 $cnt[0] = 1$。
48+
49+
接下来我们遍历数组 `nums`,用变量 $s$ 维护当前的前缀和,对于每个 $s$,我们可以计算出 $s - goal$ 出现的次数,即为以当前位置结尾的满足条件的子数组个数,累加到答案中。然后我们将 $s$ 的计数值加 $1$。
50+
51+
最终的答案即为满足条件的子数组个数。
52+
53+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
54+
55+
**方法二:双指针**
4656

4757
<!-- tabs:start -->
4858

@@ -53,12 +63,12 @@
5363
```python
5464
class Solution:
5565
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
56-
counter = Counter({0: 1})
57-
s = ans = 0
58-
for num in nums:
59-
s += num
60-
ans += counter[s - goal]
61-
counter[s] += 1
66+
cnt = Counter({0: 1})
67+
ans = s = 0
68+
for v in nums:
69+
s += v
70+
ans += cnt[s - goal]
71+
cnt[s] += 1
6272
return ans
6373
```
6474

@@ -88,15 +98,15 @@ class Solution:
8898
```java
8999
class Solution {
90100
public int numSubarraysWithSum(int[] nums, int goal) {
91-
int[] counter = new int[nums.length + 1];
92-
counter[0] = 1;
93-
int s = 0, ans = 0;
94-
for (int num : nums) {
95-
s += num;
96-
if (s >= goal) {
97-
ans += counter[s - goal];
101+
int[] cnt = new int[nums.length + 1];
102+
cnt[0] = 1;
103+
int ans = 0, s = 0;
104+
for (int v : nums) {
105+
s += v;
106+
if (s - goal >= 0) {
107+
ans += cnt[s - goal];
98108
}
99-
++counter[s];
109+
++cnt[s];
100110
}
101111
return ans;
102112
}
@@ -131,13 +141,16 @@ class Solution {
131141
class Solution {
132142
public:
133143
int numSubarraysWithSum(vector<int>& nums, int goal) {
134-
vector<int> counter(nums.size() + 1);
135-
counter[0] = 1;
136-
int s = 0, ans = 0;
137-
for (int& num : nums) {
138-
s += num;
139-
if (s >= goal) ans += counter[s - goal];
140-
++counter[s];
144+
int cnt[nums.size() + 1];
145+
memset(cnt, 0, sizeof cnt);
146+
cnt[0] = 1;
147+
int ans = 0, s = 0;
148+
for (int& v : nums) {
149+
s += v;
150+
if (s - goal >= 0) {
151+
ans += cnt[s - goal];
152+
}
153+
++cnt[s];
141154
}
142155
return ans;
143156
}
@@ -167,18 +180,15 @@ public:
167180
### **Go**
168181

169182
```go
170-
func numSubarraysWithSum(nums []int, goal int) int {
171-
counter := make([]int, len(nums)+1)
172-
counter[0] = 1
173-
s, ans := 0, 0
174-
for _, num := range nums {
175-
s += num
176-
if s >= goal {
177-
ans += counter[s-goal]
178-
}
179-
counter[s]++
183+
func numSubarraysWithSum(nums []int, goal int) (ans int) {
184+
cnt := map[int]int{0: 1}
185+
s := 0
186+
for _, v := range nums {
187+
s += v
188+
ans += cnt[s-goal]
189+
cnt[s]++
180190
}
181-
return ans
191+
return
182192
}
183193
```
184194

@@ -205,6 +215,28 @@ func numSubarraysWithSum(nums []int, goal int) int {
205215

206216
### **JavaScript**
207217

218+
```js
219+
/**
220+
* @param {number[]} nums
221+
* @param {number} goal
222+
* @return {number}
223+
*/
224+
var numSubarraysWithSum = function (nums, goal) {
225+
const cnt = new Array(nums.length + 1).fill(0);
226+
cnt[0] = 1;
227+
let ans = 0;
228+
let s = 0;
229+
for (const v of nums) {
230+
s += v;
231+
if (s >= goal) {
232+
ans += cnt[s - goal];
233+
}
234+
++cnt[s];
235+
}
236+
return ans;
237+
};
238+
```
239+
208240
```js
209241
/**
210242
* @param {number[]} nums

Diff for: solution/0900-0999/0930.Binary Subarrays With Sum/README_EN.md

+54-36
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,9 @@
4545
<p><strong>Constraints:</strong></p>
4646

4747
<ul>
48-
4948
<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
50-
5149
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
52-
5350
<li><code>0 &lt;= goal &lt;= nums.length</code></li>
54-
5551
</ul>
5652

5753
## Solutions
@@ -63,12 +59,12 @@
6359
```python
6460
class Solution:
6561
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
66-
counter = Counter({0: 1})
67-
s = ans = 0
68-
for num in nums:
69-
s += num
70-
ans += counter[s - goal]
71-
counter[s] += 1
62+
cnt = Counter({0: 1})
63+
ans = s = 0
64+
for v in nums:
65+
s += v
66+
ans += cnt[s - goal]
67+
cnt[s] += 1
7268
return ans
7369
```
7470

@@ -96,15 +92,15 @@ class Solution:
9692
```java
9793
class Solution {
9894
public int numSubarraysWithSum(int[] nums, int goal) {
99-
int[] counter = new int[nums.length + 1];
100-
counter[0] = 1;
101-
int s = 0, ans = 0;
102-
for (int num : nums) {
103-
s += num;
104-
if (s >= goal) {
105-
ans += counter[s - goal];
95+
int[] cnt = new int[nums.length + 1];
96+
cnt[0] = 1;
97+
int ans = 0, s = 0;
98+
for (int v : nums) {
99+
s += v;
100+
if (s - goal >= 0) {
101+
ans += cnt[s - goal];
106102
}
107-
++counter[s];
103+
++cnt[s];
108104
}
109105
return ans;
110106
}
@@ -139,13 +135,16 @@ class Solution {
139135
class Solution {
140136
public:
141137
int numSubarraysWithSum(vector<int>& nums, int goal) {
142-
vector<int> counter(nums.size() + 1);
143-
counter[0] = 1;
144-
int s = 0, ans = 0;
145-
for (int& num : nums) {
146-
s += num;
147-
if (s >= goal) ans += counter[s - goal];
148-
++counter[s];
138+
int cnt[nums.size() + 1];
139+
memset(cnt, 0, sizeof cnt);
140+
cnt[0] = 1;
141+
int ans = 0, s = 0;
142+
for (int& v : nums) {
143+
s += v;
144+
if (s - goal >= 0) {
145+
ans += cnt[s - goal];
146+
}
147+
++cnt[s];
149148
}
150149
return ans;
151150
}
@@ -175,18 +174,15 @@ public:
175174
### **Go**
176175

177176
```go
178-
func numSubarraysWithSum(nums []int, goal int) int {
179-
counter := make([]int, len(nums)+1)
180-
counter[0] = 1
181-
s, ans := 0, 0
182-
for _, num := range nums {
183-
s += num
184-
if s >= goal {
185-
ans += counter[s-goal]
186-
}
187-
counter[s]++
177+
func numSubarraysWithSum(nums []int, goal int) (ans int) {
178+
cnt := map[int]int{0: 1}
179+
s := 0
180+
for _, v := range nums {
181+
s += v
182+
ans += cnt[s-goal]
183+
cnt[s]++
188184
}
189-
return ans
185+
return
190186
}
191187
```
192188

@@ -213,6 +209,28 @@ func numSubarraysWithSum(nums []int, goal int) int {
213209

214210
### **JavaScript**
215211

212+
```js
213+
/**
214+
* @param {number[]} nums
215+
* @param {number} goal
216+
* @return {number}
217+
*/
218+
var numSubarraysWithSum = function (nums, goal) {
219+
const cnt = new Array(nums.length + 1).fill(0);
220+
cnt[0] = 1;
221+
let ans = 0;
222+
let s = 0;
223+
for (const v of nums) {
224+
s += v;
225+
if (s >= goal) {
226+
ans += cnt[s - goal];
227+
}
228+
++cnt[s];
229+
}
230+
return ans;
231+
};
232+
```
233+
216234
```js
217235
/**
218236
* @param {number[]} nums

0 commit comments

Comments
 (0)