Skip to content

feat: add solutions to lc problem: No.2348 #4655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 53 additions & 22 deletions solution/2300-2399/2348.Number of Zero-Filled Subarrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)$

相似题目:

Expand All @@ -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
```

Expand All @@ -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;
}
Expand All @@ -118,9 +124,12 @@ public:
long long zeroFilledSubarray(vector<int>& 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;
}
Expand All @@ -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
}
Expand All @@ -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<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [1,3,0,0,2,0,0,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
<strong>Explanation:</strong>
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.</pre>
Expand Down Expand Up @@ -68,7 +68,18 @@ There is no occurrence of a subarray with a size more than 3 filled with 0. Ther

<!-- solution:start -->

### 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)

<!-- tabs:start -->

Expand All @@ -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
```

Expand All @@ -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;
}
Expand All @@ -108,9 +125,12 @@ public:
long long zeroFilledSubarray(vector<int>& 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;
}
Expand All @@ -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
}
Expand All @@ -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<i32>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ class Solution {
long long zeroFilledSubarray(vector<int>& 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;
}
};
};
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
impl Solution {
pub fn zero_filled_subarray(nums: Vec<i32>) -> 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
}
}
Original file line number Diff line number Diff line change
@@ -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;
}