diff --git a/leetcode/2348/2348. Number of Zero-Filled Subarrays.go b/leetcode/2348/2348. Number of Zero-Filled Subarrays.go new file mode 100644 index 0000000..c2c6b4b --- /dev/null +++ b/leetcode/2348/2348. Number of Zero-Filled Subarrays.go @@ -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 +} diff --git a/leetcode/2348/2348. Number of Zero-Filled Subarrays_test.go b/leetcode/2348/2348. Number of Zero-Filled Subarrays_test.go new file mode 100644 index 0000000..711152b --- /dev/null +++ b/leetcode/2348/2348. Number of Zero-Filled Subarrays_test.go @@ -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) + } + } +}