当前位置:网站首页>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;
}
};
边栏推荐
- Thingsboard - rest API obtains and refreshes tokens
- Paper review: U2 net, u-net composed of u-net
- [micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS
- Qiniu cloud uploads video to get the first frame of video
- 【Redis实现秒杀业务③】超卖问题之乐观锁具体实现
- 移动安全工具-jar
- 打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?
- How to reduce the font size of custom controls (optimize the round dot progress bar)
- Ott marketing is booming. How should businesses invest?
- What is test development? Can you find a job at this stage?
猜你喜欢

MySQL log management
Paper review: U2 net, u-net composed of u-net

Decoupling pages and components using lifecycle
Is it so difficult to calculate the REM size of the web page according to the design draft?

Adding, deleting, modifying and checking in low build code
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform

JMeter socket connection sends data
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!
How can I persuade leaders to use DDD to construct the liver project?

The basic principle and application of iterator and enhanced for
随机推荐
placeholder
打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?
Is it so difficult to calculate the REM size of the web page according to the design draft?
Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
Helm chart仓库操作
D does not require opapply() as a domain
What is test development? Can you find a job at this stage?
Only positive integers can be entered in the text box
Previous basic review (link)
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
How to reduce the font size of custom controls (optimize the round dot progress bar)
Go crawler framework -colly actual combat (II) -- Douban top250 crawling
VNC viewer remote connection raspberry pie without display
腾讯云国际云服务器网络访问丢包问题解决办法
傳輸層 以字節為單比特的滑動窗口技術
2019 summary and 2020 outlook
JDBC - database connection
Discrete mathematics and its application detailed explanation of exercises in the final exam of spring and summer semester of 2018-2019 academic year
Tiktok wallpaper applet v1.0.2 function, new arrival function
Thingsboard - rest API obtains and refreshes tokens