From d9cf0f6edf835ca44286e0272b44647e856af270 Mon Sep 17 00:00:00 2001 From: Ashing Zheng Date: Wed, 10 Jul 2024 01:32:40 +0800 Subject: [PATCH] feat: 2348. Number of Zero-Filled Subarrays (#225) Signed-off-by: ashing --- .../2348. Number of Zero-Filled Subarrays.go | 14 +++++++++++ ...8. Number of Zero-Filled Subarrays_test.go | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 leetcode/2348/2348. Number of Zero-Filled Subarrays.go create mode 100644 leetcode/2348/2348. Number of Zero-Filled Subarrays_test.go 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) + } + } +}