Skip to content

Commit b66fbb1

Browse files
committed
930 accepted.
1 parent 17354d4 commit b66fbb1

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Algorithms/0930.binary-subarrays-with-sum/binary-subarrays-with-sum.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package problem0930
22

33
func numSubarraysWithSum(A []int, S int) int {
4+
if S == 0 {
5+
return zero(A)
6+
}
7+
48
size := len(A)
59
i, j, sum := 0, 0, 0
610

@@ -10,9 +14,9 @@ func numSubarraysWithSum(A []int, S int) int {
1014
}
1115

1216
res := 0
13-
for i < size && j <= size {
17+
for i < size && j <= size && sum == S {
1418
left := 1
15-
for i < size && A[i] == 0 {
19+
for i < j && A[i] == 0 {
1620
i++
1721
left++
1822
}
@@ -31,3 +35,17 @@ func numSubarraysWithSum(A []int, S int) int {
3135

3236
return res
3337
}
38+
39+
func zero(A []int) int {
40+
res := 0
41+
tmp := 0
42+
for _, a := range A {
43+
if a == 1 {
44+
tmp = 0
45+
} else {
46+
tmp++
47+
res += tmp
48+
}
49+
}
50+
return res
51+
}

Algorithms/0930.binary-subarrays-with-sum/binary-subarrays-with-sum_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ var tcs = []struct {
1313
ans int
1414
}{
1515

16+
{
17+
[]int{0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
18+
0,
19+
27,
20+
},
21+
22+
{
23+
[]int{0, 0, 0, 0, 1},
24+
2,
25+
0,
26+
},
27+
1628
{
1729
[]int{0, 0, 0, 0, 0},
1830
0,

0 commit comments

Comments
 (0)