diff --git a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/README.md b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/README.md index c08d483e02ed0..2f74010b9b27f 100644 --- a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/README.md +++ b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/README.md @@ -69,11 +69,11 @@ tags: ### 方法一:遍历计数 -我们可以遍历数组 `nums`,用变量 $cnt$ 记录当前连续的 `0` 的个数,用变量 $ans$ 记录答案。当遍历到 `nums[i]` 时,如果 `nums[i]` 为 `0`,则 $cnt$ 自增 $1$,否则 $cnt$ 置 $0$。然后将 $cnt$ 累加到答案 $ans$ 中。 +我们遍历数组 $\textit{nums}$,用变量 $\textit{cnt}$ 记录当前连续的 $0$ 的个数。那么对于当前遍历到的元素 $x$,如果 $x$ 为 $0$,则 $\textit{cnt}$ 自增 $1$,以当前 $x$ 为结尾的全 $0$ 子数组的个数为 $\textit{cnt}$,将其累加到答案中。否则,我们将 $\textit{cnt}$ 置为 $0$。 -最后,返回 $ans$ 即可。 +遍历结束后,返回答案即可。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `nums` 的长度。 +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 相似题目: @@ -88,9 +88,12 @@ tags: class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: ans = cnt = 0 - for v in nums: - cnt = 0 if v else cnt + 1 - ans += cnt + for x in nums: + if x == 0: + cnt += 1 + ans += cnt + else: + cnt = 0 return ans ``` @@ -101,9 +104,12 @@ class Solution { public long zeroFilledSubarray(int[] nums) { long ans = 0; int cnt = 0; - for (int v : nums) { - cnt = v != 0 ? 0 : cnt + 1; - ans += cnt; + for (int x : nums) { + if (x == 0) { + ans += ++cnt; + } else { + cnt = 0; + } } return ans; } @@ -118,9 +124,12 @@ public: long long zeroFilledSubarray(vector& nums) { long long ans = 0; int cnt = 0; - for (int& v : nums) { - cnt = v ? 0 : cnt + 1; - ans += cnt; + for (int x : nums) { + if (x == 0) { + ans += ++cnt; + } else { + cnt = 0; + } } return ans; } @@ -132,13 +141,13 @@ public: ```go func zeroFilledSubarray(nums []int) (ans int64) { cnt := 0 - for _, v := range nums { - if v != 0 { - cnt = 0 - } else { + for _, x := range nums { + if x == 0 { cnt++ + ans += int64(cnt) + } else { + cnt = 0 } - ans += int64(cnt) } return } @@ -148,16 +157,38 @@ func zeroFilledSubarray(nums []int) (ans int64) { ```ts function zeroFilledSubarray(nums: number[]): number { - let ans = 0; - let cnt = 0; - for (const v of nums) { - cnt = v ? 0 : cnt + 1; - ans += cnt; + let [ans, cnt] = [0, 0]; + for (const x of nums) { + if (!x) { + ans += ++cnt; + } else { + cnt = 0; + } } return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn zero_filled_subarray(nums: Vec) -> i64 { + let mut ans: i64 = 0; + let mut cnt: i64 = 0; + for x in nums { + if x == 0 { + cnt += 1; + ans += cnt; + } else { + cnt = 0; + } + } + ans + } +} +``` + diff --git a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/README_EN.md b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/README_EN.md index 126a680583ed6..65887ef4f9bf5 100644 --- a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/README_EN.md +++ b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/README_EN.md @@ -29,7 +29,7 @@ tags:
 Input: nums = [1,3,0,0,2,0,0,4]
 Output: 6
-Explanation: 
+Explanation:
 There are 4 occurrences of [0] as a subarray.
 There are 2 occurrences of [0,0] as a subarray.
 There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.
@@ -68,7 +68,18 @@ There is no occurrence of a subarray with a size more than 3 filled with 0. Ther -### Solution 1 +### Solution 1: Traversal and Counting + +We traverse the array $\textit{nums}$ and use a variable $\textit{cnt}$ to record the current number of consecutive $0$s. For the current element $x$ we are traversing, if $x$ is $0$, then $\textit{cnt}$ is incremented by $1$, and the number of all-zero subarrays ending with the current $x$ is $\textit{cnt}$, which we add to the answer. Otherwise, we set $\textit{cnt}$ to $0$. + +After the traversal, we return the answer. + +Time complexity $O(n)$, where $n$ is the length of the array $\textit{nums}$. Space complexity $O(1)$. + +Similar problems: + +- [413. Arithmetic Slices](https://github.com/doocs/leetcode/blob/main/solution/0400-0499/0413.Arithmetic%20Slices/README_EN.md) +- [1513. Number of Substrings With Only 1s](https://github.com/doocs/leetcode/blob/main/solution/1500-1599/1513.Number%20of%20Substrings%20With%20Only%201s/README_EN.md) @@ -78,9 +89,12 @@ There is no occurrence of a subarray with a size more than 3 filled with 0. Ther class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: ans = cnt = 0 - for v in nums: - cnt = 0 if v else cnt + 1 - ans += cnt + for x in nums: + if x == 0: + cnt += 1 + ans += cnt + else: + cnt = 0 return ans ``` @@ -91,9 +105,12 @@ class Solution { public long zeroFilledSubarray(int[] nums) { long ans = 0; int cnt = 0; - for (int v : nums) { - cnt = v != 0 ? 0 : cnt + 1; - ans += cnt; + for (int x : nums) { + if (x == 0) { + ans += ++cnt; + } else { + cnt = 0; + } } return ans; } @@ -108,9 +125,12 @@ public: long long zeroFilledSubarray(vector& nums) { long long ans = 0; int cnt = 0; - for (int& v : nums) { - cnt = v ? 0 : cnt + 1; - ans += cnt; + for (int x : nums) { + if (x == 0) { + ans += ++cnt; + } else { + cnt = 0; + } } return ans; } @@ -122,13 +142,13 @@ public: ```go func zeroFilledSubarray(nums []int) (ans int64) { cnt := 0 - for _, v := range nums { - if v != 0 { - cnt = 0 - } else { + for _, x := range nums { + if x == 0 { cnt++ + ans += int64(cnt) + } else { + cnt = 0 } - ans += int64(cnt) } return } @@ -138,16 +158,38 @@ func zeroFilledSubarray(nums []int) (ans int64) { ```ts function zeroFilledSubarray(nums: number[]): number { - let ans = 0; - let cnt = 0; - for (const v of nums) { - cnt = v ? 0 : cnt + 1; - ans += cnt; + let [ans, cnt] = [0, 0]; + for (const x of nums) { + if (!x) { + ans += ++cnt; + } else { + cnt = 0; + } } return ans; } ``` +#### Rust + +```rust +impl Solution { + pub fn zero_filled_subarray(nums: Vec) -> i64 { + let mut ans: i64 = 0; + let mut cnt: i64 = 0; + for x in nums { + if x == 0 { + cnt += 1; + ans += cnt; + } else { + cnt = 0; + } + } + ans + } +} +``` + diff --git a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.cpp b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.cpp index 4fed175e0cd35..3fde5919614f9 100644 --- a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.cpp +++ b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.cpp @@ -3,10 +3,13 @@ class Solution { long long zeroFilledSubarray(vector& nums) { long long ans = 0; int cnt = 0; - for (int& v : nums) { - cnt = v ? 0 : cnt + 1; - ans += cnt; + for (int x : nums) { + if (x == 0) { + ans += ++cnt; + } else { + cnt = 0; + } } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.go b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.go index 7ccef4f11018c..e7d97576bbcc0 100644 --- a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.go +++ b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.go @@ -1,12 +1,12 @@ func zeroFilledSubarray(nums []int) (ans int64) { cnt := 0 - for _, v := range nums { - if v != 0 { - cnt = 0 - } else { + for _, x := range nums { + if x == 0 { cnt++ + ans += int64(cnt) + } else { + cnt = 0 } - ans += int64(cnt) } return -} \ No newline at end of file +} diff --git a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.java b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.java index d069d292024c1..6ed8d114f0799 100644 --- a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.java +++ b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.java @@ -2,10 +2,13 @@ class Solution { public long zeroFilledSubarray(int[] nums) { long ans = 0; int cnt = 0; - for (int v : nums) { - cnt = v != 0 ? 0 : cnt + 1; - ans += cnt; + for (int x : nums) { + if (x == 0) { + ans += ++cnt; + } else { + cnt = 0; + } } return ans; } -} \ No newline at end of file +} diff --git a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.py b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.py index a8bd276e41054..a156239ecd68b 100644 --- a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.py +++ b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.py @@ -1,7 +1,10 @@ class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: ans = cnt = 0 - for v in nums: - cnt = 0 if v else cnt + 1 - ans += cnt + for x in nums: + if x == 0: + cnt += 1 + ans += cnt + else: + cnt = 0 return ans diff --git a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.rs b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.rs new file mode 100644 index 0000000000000..bf990286c6cfb --- /dev/null +++ b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.rs @@ -0,0 +1,15 @@ +impl Solution { + pub fn zero_filled_subarray(nums: Vec) -> i64 { + let mut ans: i64 = 0; + let mut cnt: i64 = 0; + for x in nums { + if x == 0 { + cnt += 1; + ans += cnt; + } else { + cnt = 0; + } + } + ans + } +} diff --git a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.ts b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.ts index 9d01cfd3d2df3..a6dc7b08cbe06 100644 --- a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.ts +++ b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.ts @@ -1,9 +1,11 @@ function zeroFilledSubarray(nums: number[]): number { - let ans = 0; - let cnt = 0; - for (const v of nums) { - cnt = v ? 0 : cnt + 1; - ans += cnt; + let [ans, cnt] = [0, 0]; + for (const x of nums) { + if (!x) { + ans += ++cnt; + } else { + cnt = 0; + } } return ans; }