-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 2348. Number of Zero-Filled Subarrays (#225)
Signed-off-by: ashing <[email protected]>
- Loading branch information
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
leetcode/2348/2348. Number of Zero-Filled Subarrays_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |