Skip to content

Commit 58c87af

Browse files
committedDec 30, 2021
feat: add solutions to lc problem: No.0930
No.0930.Binary Subarrays With Sum
1 parent 235dac1 commit 58c87af

File tree

7 files changed

+429
-36
lines changed

7 files changed

+429
-36
lines changed
 

‎solution/0900-0999/0930.Binary Subarrays With Sum/README.md

+172
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,194 @@
3636

3737
<!-- 这里可写通用的实现逻辑 -->
3838

39+
前缀和 / 双指针。
40+
3941
<!-- tabs:start -->
4042

4143
### **Python3**
4244

4345
<!-- 这里可写当前语言的特殊实现逻辑 -->
4446

4547
```python
48+
class Solution:
49+
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
50+
counter = Counter({0: 1})
51+
s = ans = 0
52+
for num in nums:
53+
s += num
54+
ans += counter[s - goal]
55+
counter[s] += 1
56+
return ans
57+
```
4658

59+
```python
60+
class Solution:
61+
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
62+
i1 = i2 = s1 = s2 = j = ans = 0
63+
n = len(nums)
64+
while j < n:
65+
s1 += nums[j]
66+
s2 += nums[j]
67+
while i1 <= j and s1 > goal:
68+
s1 -= nums[i1]
69+
i1 += 1
70+
while i2 <= j and s2 >= goal:
71+
s2 -= nums[i2]
72+
i2 += 1
73+
ans += i2 - i1
74+
j += 1
75+
return ans
4776
```
4877

4978
### **Java**
5079

5180
<!-- 这里可写当前语言的特殊实现逻辑 -->
5281

5382
```java
83+
class Solution {
84+
public int numSubarraysWithSum(int[] nums, int goal) {
85+
int[] counter = new int[nums.length + 1];
86+
counter[0] = 1;
87+
int s = 0, ans = 0;
88+
for (int num : nums) {
89+
s += num;
90+
if (s >= goal) {
91+
ans += counter[s - goal];
92+
}
93+
++counter[s];
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
```java
101+
class Solution {
102+
public int numSubarraysWithSum(int[] nums, int goal) {
103+
int i1 = 0, i2 = 0, s1 = 0, s2 = 0, j = 0, ans = 0;
104+
int n = nums.length;
105+
while (j < n) {
106+
s1 += nums[j];
107+
s2 += nums[j];
108+
while (i1 <= j && s1 > goal) {
109+
s1 -= nums[i1++];
110+
}
111+
while (i2 <= j && s2 >= goal) {
112+
s2 -= nums[i2++];
113+
}
114+
ans += i2 - i1;
115+
++j;
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int numSubarraysWithSum(vector<int>& nums, int goal) {
128+
vector<int> counter(nums.size() + 1);
129+
counter[0] = 1;
130+
int s = 0, ans = 0;
131+
for (int& num : nums)
132+
{
133+
s += num;
134+
if (s >= goal) ans += counter[s - goal];
135+
++counter[s];
136+
}
137+
return ans;
138+
}
139+
};
140+
```
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
int numSubarraysWithSum(vector<int>& nums, int goal) {
146+
int i1 = 0, i2 = 0, s1 = 0, s2 = 0, j = 0, ans = 0;
147+
int n = nums.size();
148+
while (j < n)
149+
{
150+
s1 += nums[j];
151+
s2 += nums[j];
152+
while (i1 <= j && s1 > goal) s1 -= nums[i1++];
153+
while (i2 <= j && s2 >= goal) s2 -= nums[i2++];
154+
ans += i2 - i1;
155+
++j;
156+
}
157+
return ans;
158+
}
159+
};
160+
```
161+
162+
### **Go**
163+
164+
```go
165+
func numSubarraysWithSum(nums []int, goal int) int {
166+
counter := make([]int, len(nums)+1)
167+
counter[0] = 1
168+
s, ans := 0, 0
169+
for _, num := range nums {
170+
s += num
171+
if s >= goal {
172+
ans += counter[s-goal]
173+
}
174+
counter[s]++
175+
}
176+
return ans
177+
}
178+
```
179+
180+
```go
181+
func numSubarraysWithSum(nums []int, goal int) int {
182+
i1, i2, s1, s2, j, ans, n := 0, 0, 0, 0, 0, 0, len(nums)
183+
for j < n {
184+
s1 += nums[j]
185+
s2 += nums[j]
186+
for i1 <= j && s1 > goal {
187+
s1 -= nums[i1]
188+
i1++
189+
}
190+
for i2 <= j && s2 >= goal {
191+
s2 -= nums[i2]
192+
i2++
193+
}
194+
ans += i2 - i1
195+
j++
196+
}
197+
return ans
198+
}
199+
```
54200

201+
### **JavaScript**
202+
203+
```js
204+
/**
205+
* @param {number[]} nums
206+
* @param {number} goal
207+
* @return {number}
208+
*/
209+
var numSubarraysWithSum = function (nums, goal) {
210+
let i1 = 0,
211+
i2 = 0,
212+
s1 = 0,
213+
s2 = 0,
214+
j = 0,
215+
ans = 0;
216+
const n = nums.length;
217+
while (j < n) {
218+
s1 += nums[j];
219+
s2 += nums[j];
220+
while (i1 <= j && s1 > goal) s1 -= nums[i1++];
221+
while (i2 <= j && s2 >= goal) s2 -= nums[i2++];
222+
ans += i2 - i1;
223+
++j;
224+
}
225+
return ans;
226+
};
55227
```
56228

57229
### **...**

‎solution/0900-0999/0930.Binary Subarrays With Sum/README_EN.md

+170
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,183 @@ The 4 subarrays are bolded below:
4747
### **Python3**
4848

4949
```python
50+
class Solution:
51+
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
52+
counter = Counter({0: 1})
53+
s = ans = 0
54+
for num in nums:
55+
s += num
56+
ans += counter[s - goal]
57+
counter[s] += 1
58+
return ans
59+
```
5060

61+
```python
62+
class Solution:
63+
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
64+
i1 = i2 = s1 = s2 = j = ans = 0
65+
n = len(nums)
66+
while j < n:
67+
s1 += nums[j]
68+
s2 += nums[j]
69+
while i1 <= j and s1 > goal:
70+
s1 -= nums[i1]
71+
i1 += 1
72+
while i2 <= j and s2 >= goal:
73+
s2 -= nums[i2]
74+
i2 += 1
75+
ans += i2 - i1
76+
j += 1
77+
return ans
5178
```
5279

5380
### **Java**
5481

5582
```java
83+
class Solution {
84+
public int numSubarraysWithSum(int[] nums, int goal) {
85+
int[] counter = new int[nums.length + 1];
86+
counter[0] = 1;
87+
int s = 0, ans = 0;
88+
for (int num : nums) {
89+
s += num;
90+
if (s >= goal) {
91+
ans += counter[s - goal];
92+
}
93+
++counter[s];
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
```java
101+
class Solution {
102+
public int numSubarraysWithSum(int[] nums, int goal) {
103+
int i1 = 0, i2 = 0, s1 = 0, s2 = 0, j = 0, ans = 0;
104+
int n = nums.length;
105+
while (j < n) {
106+
s1 += nums[j];
107+
s2 += nums[j];
108+
while (i1 <= j && s1 > goal) {
109+
s1 -= nums[i1++];
110+
}
111+
while (i2 <= j && s2 >= goal) {
112+
s2 -= nums[i2++];
113+
}
114+
ans += i2 - i1;
115+
++j;
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int numSubarraysWithSum(vector<int>& nums, int goal) {
128+
vector<int> counter(nums.size() + 1);
129+
counter[0] = 1;
130+
int s = 0, ans = 0;
131+
for (int& num : nums)
132+
{
133+
s += num;
134+
if (s >= goal) ans += counter[s - goal];
135+
++counter[s];
136+
}
137+
return ans;
138+
}
139+
};
140+
```
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
int numSubarraysWithSum(vector<int>& nums, int goal) {
146+
int i1 = 0, i2 = 0, s1 = 0, s2 = 0, j = 0, ans = 0;
147+
int n = nums.size();
148+
while (j < n)
149+
{
150+
s1 += nums[j];
151+
s2 += nums[j];
152+
while (i1 <= j && s1 > goal) s1 -= nums[i1++];
153+
while (i2 <= j && s2 >= goal) s2 -= nums[i2++];
154+
ans += i2 - i1;
155+
++j;
156+
}
157+
return ans;
158+
}
159+
};
160+
```
161+
162+
### **Go**
163+
164+
```go
165+
func numSubarraysWithSum(nums []int, goal int) int {
166+
counter := make([]int, len(nums)+1)
167+
counter[0] = 1
168+
s, ans := 0, 0
169+
for _, num := range nums {
170+
s += num
171+
if s >= goal {
172+
ans += counter[s-goal]
173+
}
174+
counter[s]++
175+
}
176+
return ans
177+
}
178+
```
179+
180+
```go
181+
func numSubarraysWithSum(nums []int, goal int) int {
182+
i1, i2, s1, s2, j, ans, n := 0, 0, 0, 0, 0, 0, len(nums)
183+
for j < n {
184+
s1 += nums[j]
185+
s2 += nums[j]
186+
for i1 <= j && s1 > goal {
187+
s1 -= nums[i1]
188+
i1++
189+
}
190+
for i2 <= j && s2 >= goal {
191+
s2 -= nums[i2]
192+
i2++
193+
}
194+
ans += i2 - i1
195+
j++
196+
}
197+
return ans
198+
}
199+
```
56200

201+
### **JavaScript**
202+
203+
```js
204+
/**
205+
* @param {number[]} nums
206+
* @param {number} goal
207+
* @return {number}
208+
*/
209+
var numSubarraysWithSum = function (nums, goal) {
210+
let i1 = 0,
211+
i2 = 0,
212+
s1 = 0,
213+
s2 = 0,
214+
j = 0,
215+
ans = 0;
216+
const n = nums.length;
217+
while (j < n) {
218+
s1 += nums[j];
219+
s2 += nums[j];
220+
while (i1 <= j && s1 > goal) s1 -= nums[i1++];
221+
while (i2 <= j && s2 >= goal) s2 -= nums[i2++];
222+
ans += i2 - i1;
223+
++j;
224+
}
225+
return ans;
226+
};
57227
```
58228

59229
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int numSubarraysWithSum(vector<int>& nums, int goal) {
4+
int i1 = 0, i2 = 0, s1 = 0, s2 = 0, j = 0, ans = 0;
5+
int n = nums.size();
6+
while (j < n)
7+
{
8+
s1 += nums[j];
9+
s2 += nums[j];
10+
while (i1 <= j && s1 > goal) s1 -= nums[i1++];
11+
while (i2 <= j && s2 >= goal) s2 -= nums[i2++];
12+
ans += i2 - i1;
13+
++j;
14+
}
15+
return ans;
16+
}
17+
};

0 commit comments

Comments
 (0)