当前位置:网站首页>Leetcode 1248. 统计「优美子数组」(害,突然发现只会暴力枚举了)
Leetcode 1248. 统计「优美子数组」(害,突然发现只会暴力枚举了)
2022-06-24 20:08:00 【我不是萧海哇~~~~】
给你一个整数数组 nums 和一个整数 k。如果某个连续子数组中恰好有 k 个奇数数字,我们就认为这个子数组是「优美子数组」。
请返回这个数组中 「优美子数组」 的数目。
示例 1:
输入:nums = [1,1,2,1,1], k = 3
输出:2
解释:包含 3 个奇数的子数组是 [1,1,2,1] 和 [1,2,1,1] 。
示例 2:
输入:nums = [2,4,6], k = 1
输出:0
解释:数列中不包含任何奇数,所以不存在优美子数组。
示例 3:
输入:nums = [2,2,2,1,2,2,1,2,2,2], k = 2
输出:16
提示:
- 1 <= nums.length <= 50000
- 1 <= nums[i] <= 10^5
- 1 <= k <= nums.length
Code:
class Solution {
public:
int numberOfSubarrays(vector<int>& nums, int k) {
int res=0;
for(int i=0;i<nums.size();i++)
{
int cnt=0;
for(int j=i;j<nums.size();j++)
{
if(nums[j]%2)
{
cnt++;
}
if(cnt>k)
{
break;
}
if(cnt==k)
{
res++;
}
}
}
cout<<res<<endl;
return res;
}
};
边栏推荐
- Punch smart spirit 1. The brand is attractive. What is the strength of the product?
- EVM简略
- 热力图展示相关矩阵
- How to use promise Race() and promise any() ?
- Use of JMeter easynmon
- Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
- Use coordinatorlayout+appbarlayout+collapsingtoolbarlayout to create a collapsed status bar
- Source code analysis the problem that fragments cannot be displayed in the custom ViewGroup
- Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!
- Usage of ViewModel and livedata in jetpack
猜你喜欢

百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑

Practical operation notes - notebook plus memory and ash cleaning
![[microservices sentinel] sentinel quick start | building an image | starting the console](/img/88/a01c8120f6117f1b8e4463cf6f685f.png)
[microservices sentinel] sentinel quick start | building an image | starting the console

Applet opening traffic master
Modstart: embrace new technologies and take the lead in supporting laravel 9.0

Thermodynamic diagram display correlation matrix

The acceleration of 100 km is only 5.92 seconds, and the willanda high-performance version leads with the strength of high-energy products

How to quickly open traffic master for wechat applet
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!

Custom animation (simulated win10 loading animation) - Optimization
随机推荐
The drawableleft of the custom textview in kotlin is displayed in the center together with the text
The picture of wechat official account can not be displayed normally
Custom control - round dot progress bar (imitating one key acceleration in security guard)
C program design topic 18-19 final exam exercise solutions (Part 2)
Eliminate duplicate dependencies
Helm chart warehouse operation
Tiktok wallpaper applet v1.0.2 function, new arrival function
C# 闭包的垃圾回收
Adding, deleting, modifying and checking in low build code
ros(25):rqt_image_view报错Unable to load plugin for transport ‘compressed‘, error string
MySQL log management
UE4 WebBrowser chart cannot display problems
EVM简略
移动安全工具-dex2jar
[microservices sentinel] sentinel quick start | building an image | starting the console
[redis realizes seckill service ④] one order for one person, and cannot be purchased repeatedly
Helm chart仓库操作
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
Use of JMeter easynmon
Use of file class filenamefilter & filefilter in io