当前位置:网站首页>Leetcode-6129: number of all 0 subarrays
Leetcode-6129: number of all 0 subarrays
2022-07-25 20:38:00 【Chrysanthemum headed bat】
leetcode-6129: whole 0 The number of subarrays
subject
Give you an array of integers nums , Return all as 0 Of Subarray number .
Subarray Is a sequence of consecutive non empty elements in an array .
Example 1:
Input :nums = [1,3,0,0,2,0,0,4]
Output :6
explain :
Subarray [0] There is 4 Time .
Subarray [0,0] There is 2 Time .
No length greater than 2 Of all the 0 Subarray , So we go back to 6 .
Example 2:
Input :nums = [0,0,0,2,0,0]
Output :9
explain :
Subarray [0] There is 5 Time .
Subarray [0,0] There is 3 Time .
Subarray [0,0,0] There is 1 Time .
No length greater than 3 Of all the 0 Subarray , So we go back to 9 .
Example 3:
Input :nums = [2,10,2019]
Output :0
explain : Not all 0 Subarray , So we go back to 0 .

Problem solving
Method 1 : The sliding window + A sequence of equal differences
Continuous recording 0 Array length of as well as Corresponding to the number of their occurrence
By sliding the window .
Then according to the continuous 0 The length of the array and the number of occurrences , To calculate the total 0 The number of subarrays of .
For example, for [0,0,0]
One 0 The number of by :3
Two 0 The number of :2
Three 0 The number of :1
So it's all 0 The number of subarrays of is (1+3)*3/2
Finally multiply by [0,0,0] The number of occurrences is enough
class Solution {
public:
long long zeroFilledSubarray(vector<int>& nums) {
int n=nums.size();
unordered_map<int,int> cnt;// Successive 0 Number ----> Number
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;
}
};
边栏推荐
- leetcode-114:二叉树展开为链表
- How to choose a microservice registration center?
- [leetcode] 28. Implement strstr ()
- Go language go language built-in container
- Do you still have certificates to participate in the open source community?
- leetcode-155:最小栈
- Is QQ 32-bit or 64 bit software (where to see whether the computer is 32-bit or 64 bit)
- test
- 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!
- How to use buffer queue to realize high concurrent order business (glory Collection Edition)
猜你喜欢

Kubernetes advanced part learning notes

Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important

Kubernetes进阶部分学习笔记
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET

【高等数学】【3】微分中值定理与导数的应用

FanoutExchange交换机代码教程
![Vulnhub | dc: 5 | [actual combat]](/img/c6/34117bbfb83ebdf9e619f4e4590661.png)
Vulnhub | dc: 5 | [actual combat]

Detailed explanation of document operation

程序的编译和运行

【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
随机推荐
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
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!
FanoutExchange交换机代码教程
test
Arrow parquet
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
Remote—基本原理介绍
Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
KEGG通路的从属/注释信息如何获取
从底层结构开始学习FPGA(16)----PLL/MMCM IP的定制与测试
[tensorrt] dynamic batch reasoning
RF, gbdt, xgboost feature selection methods "recommended collection"
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
JMeter - interface test
[workplace rules] it workplace rules | poor performance
Why did I choose to become a network engineer after graduating from weak current for 3 months
Dataframe first performs grouping operation and then combines output
火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
Google guava is just a brother. What is the real king of caching? (glory Collection Edition)