当前位置:网站首页>leetcode-6129:全 0 子数组的数目
leetcode-6129:全 0 子数组的数目
2022-07-25 20:36:00 【菊头蝙蝠】
leetcode-6129:全 0 子数组的数目
题目
给你一个整数数组 nums ,返回全部为 0 的 子数组 数目。
子数组 是一个数组中一段连续非空元素组成的序列。
示例 1:
输入:nums = [1,3,0,0,2,0,0,4]
输出:6
解释:
子数组 [0] 出现了 4 次。
子数组 [0,0] 出现了 2 次。
不存在长度大于 2 的全 0 子数组,所以我们返回 6 。
示例 2:
输入:nums = [0,0,0,2,0,0]
输出:9
解释:
子数组 [0] 出现了 5 次。
子数组 [0,0] 出现了 3 次。
子数组 [0,0,0] 出现了 1 次。
不存在长度大于 3 的全 0 子数组,所以我们返回 9 。
示例 3:
输入:nums = [2,10,2019]
输出:0
解释:没有全 0 子数组,所以我们返回 0 。

解题
方法一:滑动窗口+等差数列
记录连续0的数组长度 以及 对应它们出现的个数
通过滑动窗口来实现。
然后根据连续0的数组长度和出现的次数,来计算全0的子数组个数。
比如对于 [0,0,0]
一个0的个数 为:3
两个0的个数为:2
三个0的个数为:1
因此全0的子数组个数为(1+3)*3/2
最后再乘上[0,0,0]出现的次数就行了
class Solution {
public:
long long zeroFilledSubarray(vector<int>& nums) {
int n=nums.size();
unordered_map<int,int> cnt;//连续的0数量---->个数
int left=0,right=0;
while(right<n){
while(left<n&&nums[left]!=0) left++;
right=left;
while(right<n&&nums[right]==0) right++;
cnt[right-left]++;
left=right;
}
long long res=0;
for(auto& it:cnt){
res+=(1ll+it.first)*it.first/2 *it.second;
}
return res;
}
};
边栏推荐
- Is QQ 32-bit or 64 bit software (where to see whether the computer is 32-bit or 64 bit)
- 【高等数学】【5】定积分及应用
- Prescan quick start to master the special functions of prescan track editing in lecture 18
- 使用cookie登录百度网盘(网站使用cookie)
- RF、GBDT、XGboost特征选择方法「建议收藏」
- Struct, enum type and union
- JVM (XXIII) -- JVM runtime parameters
- 【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
- 【单细胞高级绘图】07.KEGG富集结果展示
- Embedded development: embedded foundation -- threads and tasks
猜你喜欢

Detailed explanation of document operation

9. < tag dynamic programming and subsequence, subarray> lt.718. Longest repeated subarray + lt.1143. Longest common subsequence
![[leetcode] 28. Implement strstr ()](/img/87/0af2da458e53d31012d6535cc132bb.png)
[leetcode] 28. Implement strstr ()

Fanoutexchange switch code tutorial

雷达水位计的工作原理及安装维护注意事项

LeetCode通关:哈希表六连,这个还真有点简单

Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!

Key network protocols in tcp/ip four layer model

Vivo official website app full model UI adaptation scheme

【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
随机推荐
结构体,枚举类型与联合体
[advanced mathematics] [1] function, limit, continuity
Introduction and construction of consul Registration Center
Web crawler principle analysis "suggestions collection"
Docker builds redis cluster
Aircraft PID control (rotor flight control)
C language file reading and writing
Network protocol: TCP part2
Why did I choose to become a network engineer after graduating from weak current for 3 months
SecureCRT garbled code solution [easy to understand]
[Infographics Show] 248 Public Domain Name
【单细胞高级绘图】07.KEGG富集结果展示
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
Chapter VI modified specification (SPEC) class
[advanced mathematics] [8] differential equation
2022.7.24-----leetcode.1184
Struct, enum type and union
使用cookie登录百度网盘(网站使用cookie)
Increase swap space
Apache Mina framework "suggestions collection"