当前位置:网站首页>Leetcode 1248. Statistics of "graceful subarray" (harm, suddenly found that it can only enumerate violently)
Leetcode 1248. Statistics of "graceful subarray" (harm, suddenly found that it can only enumerate violently)
2022-06-25 00:57:00 【I'm not xiaohaiwa~~~~】
Give you an array of integers nums And an integer k. If there happens to be... In a continuous subarray k It's an odd number , We think that this subarray is 「 Graceful subarray 」.
Please return to this array 「 Graceful subarray 」 Number of .
Example 1:
Input :nums = [1,1,2,1,1], k = 3
Output :2
explain : contain 3 An odd subarray is [1,1,2,1] and [1,2,1,1] .
Example 2:
Input :nums = [2,4,6], k = 1
Output :0
explain : There are no odd numbers in the sequence , So there's no graceful subarray .
Example 3:
Input :nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output :16
Tips :
- 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;
}
};
边栏推荐
- 在企业级开发过程中我发现有位同事用select * from where 条件 for update
- Previous basic review
- Apk decompiled method (not confused)
- Thingsboard - rest API obtains and refreshes tokens
- How to use promise Race() and promise any() ?
- The basic principle and application of iterator and enhanced for
- C# 闭包的垃圾回收
- [leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
- [micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS
- 打卡smart精灵#1,品牌不缺吸引力,产品本身实力如何?
猜你喜欢

The basic principle and application of iterator and enhanced for
![[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS](/img/fc/6b36759570b9b1007d2640009576d9.png)
[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS

The interview questions and answers for the high-frequency software test of Dachang help you prepare for the golden nine silver ten

Custom animation (simulated win10 loading animation) - Optimization

Binder mechanism and Aidl communication example

iNFTnews | 国内NFT发展仅限于数字藏品吗?
![[microservices sentinel] cluster link | microservices cluster environment construction](/img/75/3cc6abf1e72d651b082415db9db46c.png)
[microservices sentinel] cluster link | microservices cluster environment construction

Mobile security tool jar

Applet opening traffic master

JMeter socket connection sends data
随机推荐
Applet opening traffic master
Design scheme of authority management of fusion model
The basic principle and application of iterator and enhanced for
@mysql
2022年全国最新消防设施操作员(高级消防设施操作员)模拟题及答案
adb shell getevent
2022安全员-C证考试模拟100题及在线模拟考试
D manual destruction may violate memory security
2021-02-15
How to use promise Race() and promise any() ?
Custom control - round dot progress bar (imitating one key acceleration in security guard)
Network request -volley
A small program written this week
Usage of ViewModel and livedata in jetpack
在企业级开发过程中我发现有位同事用select * from where 条件 for update
Fuxin Kunpeng joins in, and dragon lizard community welcomes a new partner in format document technical service
A website for programmers with a monthly salary of 30K
placeholder
[redis realizes seckill service ②] solution to oversold problem
Use and click of multitypeadapter in recycleview