diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/num_zero_filled_subarr.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/num_zero_filled_subarr.cpp')
-rw-r--r-- | other/num_zero_filled_subarr.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/other/num_zero_filled_subarr.cpp b/other/num_zero_filled_subarr.cpp new file mode 100644 index 0000000..8ba45f3 --- /dev/null +++ b/other/num_zero_filled_subarr.cpp @@ -0,0 +1,29 @@ +// 2348. Number of Zero-Filled Subarrays +#include "leetcode.h" + +// given integer array 'nums', return number of subarrays filled with zero. a +// subarray is a contiguous non-empty sequence of elements from within an array + +class Solution { +public: + long long zeroFilledSubarray(vector<int> &nums) { + long long ans = 0; + short n = nums.size(); + for (int i = 0, j = 0; i < n; ++i) { + if (nums[i] != 0) + j = i + 1; + ans += i - j + 1; + } + return ans; + } +}; + +int main() { + Solution obj; + vector<int> n1 = {1, 3, 0, 0, 2, 0, 0, 4}; + vector<int> n2 = {0, 0, 0, 2, 0, 0}; + vector<int> n3 = {2, 10, 2019}; + printf("%lld\n", obj.zeroFilledSubarray(n1)); // expect: 6 + printf("%lld\n", obj.zeroFilledSubarray(n2)); // expect: 9 + printf("%lld\n", obj.zeroFilledSubarray(n3)); // expect: 0 +} |