Skip to content

Commit e2331ba

Browse files
authored
feat: add solutions to lc problem: No.2348 (#4655)
No.2348.2348.Number of Zero-Filled Subarrays
1 parent b7fad5e commit e2331ba

File tree

8 files changed

+164
-65
lines changed

8 files changed

+164
-65
lines changed

solution/2300-2399/2348.Number of Zero-Filled Subarrays/README.md

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ tags:
6969

7070
### 方法一:遍历计数
7171

72-
我们可以遍历数组 `nums`,用变量 $cnt$ 记录当前连续的 `0` 的个数,用变量 $ans$ 记录答案。当遍历到 `nums[i]`,如果 `nums[i]``0`,则 $cnt$ 自增 $1$,否则 $cnt$ 置 $0$。然后将 $cnt$ 累加到答案 $ans$ 中
72+
我们遍历数组 $\textit{nums}$,用变量 $\textit{cnt}$ 记录当前连续的 $0$ 的个数。那么对于当前遍历到的元素 $x$,如果 $x$$0$,则 $\textit{cnt}$ 自增 $1$,以当前 $x$ 为结尾的全 $0$ 子数组的个数为 $\textit{cnt}$,将其累加到答案中。否则,我们将 $\textit{cnt}$ 置为 $0$
7373

74-
最后,返回 $ans$ 即可
74+
遍历结束后,返回答案即可
7575

76-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `nums` 的长度。
76+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$
7777

7878
相似题目:
7979

@@ -88,9 +88,12 @@ tags:
8888
class Solution:
8989
def zeroFilledSubarray(self, nums: List[int]) -> int:
9090
ans = cnt = 0
91-
for v in nums:
92-
cnt = 0 if v else cnt + 1
93-
ans += cnt
91+
for x in nums:
92+
if x == 0:
93+
cnt += 1
94+
ans += cnt
95+
else:
96+
cnt = 0
9497
return ans
9598
```
9699

@@ -101,9 +104,12 @@ class Solution {
101104
public long zeroFilledSubarray(int[] nums) {
102105
long ans = 0;
103106
int cnt = 0;
104-
for (int v : nums) {
105-
cnt = v != 0 ? 0 : cnt + 1;
106-
ans += cnt;
107+
for (int x : nums) {
108+
if (x == 0) {
109+
ans += ++cnt;
110+
} else {
111+
cnt = 0;
112+
}
107113
}
108114
return ans;
109115
}
@@ -118,9 +124,12 @@ public:
118124
long long zeroFilledSubarray(vector<int>& nums) {
119125
long long ans = 0;
120126
int cnt = 0;
121-
for (int& v : nums) {
122-
cnt = v ? 0 : cnt + 1;
123-
ans += cnt;
127+
for (int x : nums) {
128+
if (x == 0) {
129+
ans += ++cnt;
130+
} else {
131+
cnt = 0;
132+
}
124133
}
125134
return ans;
126135
}
@@ -132,13 +141,13 @@ public:
132141
```go
133142
func zeroFilledSubarray(nums []int) (ans int64) {
134143
cnt := 0
135-
for _, v := range nums {
136-
if v != 0 {
137-
cnt = 0
138-
} else {
144+
for _, x := range nums {
145+
if x == 0 {
139146
cnt++
147+
ans += int64(cnt)
148+
} else {
149+
cnt = 0
140150
}
141-
ans += int64(cnt)
142151
}
143152
return
144153
}
@@ -148,16 +157,38 @@ func zeroFilledSubarray(nums []int) (ans int64) {
148157

149158
```ts
150159
function zeroFilledSubarray(nums: number[]): number {
151-
let ans = 0;
152-
let cnt = 0;
153-
for (const v of nums) {
154-
cnt = v ? 0 : cnt + 1;
155-
ans += cnt;
160+
let [ans, cnt] = [0, 0];
161+
for (const x of nums) {
162+
if (!x) {
163+
ans += ++cnt;
164+
} else {
165+
cnt = 0;
166+
}
156167
}
157168
return ans;
158169
}
159170
```
160171

172+
#### Rust
173+
174+
```rust
175+
impl Solution {
176+
pub fn zero_filled_subarray(nums: Vec<i32>) -> i64 {
177+
let mut ans: i64 = 0;
178+
let mut cnt: i64 = 0;
179+
for x in nums {
180+
if x == 0 {
181+
cnt += 1;
182+
ans += cnt;
183+
} else {
184+
cnt = 0;
185+
}
186+
}
187+
ans
188+
}
189+
}
190+
```
191+
161192
<!-- tabs:end -->
162193

163194
<!-- solution:end -->

solution/2300-2399/2348.Number of Zero-Filled Subarrays/README_EN.md

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tags:
2929
<pre>
3030
<strong>Input:</strong> nums = [1,3,0,0,2,0,0,4]
3131
<strong>Output:</strong> 6
32-
<strong>Explanation:</strong>
32+
<strong>Explanation:</strong>
3333
There are 4 occurrences of [0] as a subarray.
3434
There are 2 occurrences of [0,0] as a subarray.
3535
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.</pre>
@@ -68,7 +68,18 @@ There is no occurrence of a subarray with a size more than 3 filled with 0. Ther
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Traversal and Counting
72+
73+
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$.
74+
75+
After the traversal, we return the answer.
76+
77+
Time complexity $O(n)$, where $n$ is the length of the array $\textit{nums}$. Space complexity $O(1)$.
78+
79+
Similar problems:
80+
81+
- [413. Arithmetic Slices](https://github.com/doocs/leetcode/blob/main/solution/0400-0499/0413.Arithmetic%20Slices/README_EN.md)
82+
- [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)
7283

7384
<!-- tabs:start -->
7485

@@ -78,9 +89,12 @@ There is no occurrence of a subarray with a size more than 3 filled with 0. Ther
7889
class Solution:
7990
def zeroFilledSubarray(self, nums: List[int]) -> int:
8091
ans = cnt = 0
81-
for v in nums:
82-
cnt = 0 if v else cnt + 1
83-
ans += cnt
92+
for x in nums:
93+
if x == 0:
94+
cnt += 1
95+
ans += cnt
96+
else:
97+
cnt = 0
8498
return ans
8599
```
86100

@@ -91,9 +105,12 @@ class Solution {
91105
public long zeroFilledSubarray(int[] nums) {
92106
long ans = 0;
93107
int cnt = 0;
94-
for (int v : nums) {
95-
cnt = v != 0 ? 0 : cnt + 1;
96-
ans += cnt;
108+
for (int x : nums) {
109+
if (x == 0) {
110+
ans += ++cnt;
111+
} else {
112+
cnt = 0;
113+
}
97114
}
98115
return ans;
99116
}
@@ -108,9 +125,12 @@ public:
108125
long long zeroFilledSubarray(vector<int>& nums) {
109126
long long ans = 0;
110127
int cnt = 0;
111-
for (int& v : nums) {
112-
cnt = v ? 0 : cnt + 1;
113-
ans += cnt;
128+
for (int x : nums) {
129+
if (x == 0) {
130+
ans += ++cnt;
131+
} else {
132+
cnt = 0;
133+
}
114134
}
115135
return ans;
116136
}
@@ -122,13 +142,13 @@ public:
122142
```go
123143
func zeroFilledSubarray(nums []int) (ans int64) {
124144
cnt := 0
125-
for _, v := range nums {
126-
if v != 0 {
127-
cnt = 0
128-
} else {
145+
for _, x := range nums {
146+
if x == 0 {
129147
cnt++
148+
ans += int64(cnt)
149+
} else {
150+
cnt = 0
130151
}
131-
ans += int64(cnt)
132152
}
133153
return
134154
}
@@ -138,16 +158,38 @@ func zeroFilledSubarray(nums []int) (ans int64) {
138158

139159
```ts
140160
function zeroFilledSubarray(nums: number[]): number {
141-
let ans = 0;
142-
let cnt = 0;
143-
for (const v of nums) {
144-
cnt = v ? 0 : cnt + 1;
145-
ans += cnt;
161+
let [ans, cnt] = [0, 0];
162+
for (const x of nums) {
163+
if (!x) {
164+
ans += ++cnt;
165+
} else {
166+
cnt = 0;
167+
}
146168
}
147169
return ans;
148170
}
149171
```
150172

173+
#### Rust
174+
175+
```rust
176+
impl Solution {
177+
pub fn zero_filled_subarray(nums: Vec<i32>) -> i64 {
178+
let mut ans: i64 = 0;
179+
let mut cnt: i64 = 0;
180+
for x in nums {
181+
if x == 0 {
182+
cnt += 1;
183+
ans += cnt;
184+
} else {
185+
cnt = 0;
186+
}
187+
}
188+
ans
189+
}
190+
}
191+
```
192+
151193
<!-- tabs:end -->
152194

153195
<!-- solution:end -->

solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ class Solution {
33
long long zeroFilledSubarray(vector<int>& nums) {
44
long long ans = 0;
55
int cnt = 0;
6-
for (int& v : nums) {
7-
cnt = v ? 0 : cnt + 1;
8-
ans += cnt;
6+
for (int x : nums) {
7+
if (x == 0) {
8+
ans += ++cnt;
9+
} else {
10+
cnt = 0;
11+
}
912
}
1013
return ans;
1114
}
12-
};
15+
};
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
func zeroFilledSubarray(nums []int) (ans int64) {
22
cnt := 0
3-
for _, v := range nums {
4-
if v != 0 {
5-
cnt = 0
6-
} else {
3+
for _, x := range nums {
4+
if x == 0 {
75
cnt++
6+
ans += int64(cnt)
7+
} else {
8+
cnt = 0
89
}
9-
ans += int64(cnt)
1010
}
1111
return
12-
}
12+
}

solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ class Solution {
22
public long zeroFilledSubarray(int[] nums) {
33
long ans = 0;
44
int cnt = 0;
5-
for (int v : nums) {
6-
cnt = v != 0 ? 0 : cnt + 1;
7-
ans += cnt;
5+
for (int x : nums) {
6+
if (x == 0) {
7+
ans += ++cnt;
8+
} else {
9+
cnt = 0;
10+
}
811
}
912
return ans;
1013
}
11-
}
14+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
class Solution:
22
def zeroFilledSubarray(self, nums: List[int]) -> int:
33
ans = cnt = 0
4-
for v in nums:
5-
cnt = 0 if v else cnt + 1
6-
ans += cnt
4+
for x in nums:
5+
if x == 0:
6+
cnt += 1
7+
ans += cnt
8+
else:
9+
cnt = 0
710
return ans
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn zero_filled_subarray(nums: Vec<i32>) -> i64 {
3+
let mut ans: i64 = 0;
4+
let mut cnt: i64 = 0;
5+
for x in nums {
6+
if x == 0 {
7+
cnt += 1;
8+
ans += cnt;
9+
} else {
10+
cnt = 0;
11+
}
12+
}
13+
ans
14+
}
15+
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
function zeroFilledSubarray(nums: number[]): number {
2-
let ans = 0;
3-
let cnt = 0;
4-
for (const v of nums) {
5-
cnt = v ? 0 : cnt + 1;
6-
ans += cnt;
2+
let [ans, cnt] = [0, 0];
3+
for (const x of nums) {
4+
if (!x) {
5+
ans += ++cnt;
6+
} else {
7+
cnt = 0;
8+
}
79
}
810
return ans;
911
}

0 commit comments

Comments
 (0)