当前位置:网站首页>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;
}
};
边栏推荐
- Do you still have certificates to participate in the open source community?
- [leetcode] 28. Implement strstr ()
- Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
- [tensorrt] trtexec tool to engine
- [today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
- Log in to Baidu online disk with cookies (websites use cookies)
- Vulnhub | dc: 5 | [actual combat]
- "Share" devaxpress asp Net v22.1 latest version system environment configuration requirements
- Docker 搭建 Redis Cluster集群
- MySQL date [plus sign / +] condition filtering problem
猜你喜欢

Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
![[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay](/img/18/b06e2e5a2f76dc2da1c2374b8424b3.png)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay

"Share" devaxpress asp Net v22.1 latest version system environment configuration requirements

Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34

Today's sleep quality record 75 points
![[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console

【高等数学】【3】微分中值定理与导数的应用
![[advanced mathematics] [5] definite integral and its application](/img/b2/62748b7533982f2b864148e0857490.png)
[advanced mathematics] [5] definite integral and its application

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

Why did I choose to become a network engineer after graduating from weak current for 3 months
随机推荐
Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
[workplace rules] it workplace rules | poor performance
Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
网络RTK无人机上机测试[通俗易懂]
Detailed explanation of document operation
Docker 搭建 Redis Cluster集群
Chapter VI modified specification (SPEC) class
网络协议:TCP Part2
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
结构体,枚举类型与联合体
10. < tag dynamic programming and subsequence, subarray> lt.53. maximum subarray and + lt.392. Judge subsequence DBC
RF, gbdt, xgboost feature selection methods "recommended collection"
Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks
Is QQ 32-bit or 64 bit software (where to see whether the computer is 32-bit or 64 bit)
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
Introduction and construction of consul Registration Center
Question and answer 47: geeks have an appointment - the current monitoring system construction of CSC