Skip to content

Commit

Permalink
feat: 2348. Number of Zero-Filled Subarrays (#225)
Browse files Browse the repository at this point in the history
Signed-off-by: ashing <[email protected]>
  • Loading branch information
ronething committed Jul 9, 2024
1 parent 24f1c43 commit d9cf0f6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions leetcode/2348/2348. Number of Zero-Filled Subarrays.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package _348

func zeroFilledSubarray(nums []int) (ans int64) {
c := 0
for _, num := range nums {
if num == 0 {
c++
ans += int64(c)
} else {
c = 0
}
}
return
}
25 changes: 25 additions & 0 deletions leetcode/2348/2348. Number of Zero-Filled Subarrays_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package _348

import (
"testing"
)

func TestZeroFilledSubarray(t *testing.T) {
tests := []struct {
nums []int
expect int64
}{
{[]int{0, 0, 1, 0, 0, 0, 1}, 9},
{[]int{0, 1, 0, 1, 0, 0, 1}, 5},
{[]int{1, 1, 1, 1, 1}, 0},
{[]int{0, 0, 0, 0, 0}, 15},
{[]int{}, 0},
}

for _, test := range tests {
got := zeroFilledSubarray(test.nums)
if got != test.expect {
t.Errorf("zeroFilledSubarray(%v) = %v, expect %v", test.nums, got, test.expect)
}
}
}

0 comments on commit d9cf0f6

Please sign in to comment.