题目如下:

Given an array of integers nums and an integer k. A subarray is called nice if there are k odd numbers on it.

Return the number of nice sub-arrays.

Example 1:

Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].

Example 2:

Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.

Example 3:

Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16

Constraints:

  • 1 <= nums.length <= 50000
  • 1 <= nums[i] <= 10^5
  • 1 <= k <= nums.length

解题思路:创建一个val数组,val[i]表示从0~i区间内奇数的个数。如果要求i~j区间内奇数的个数,似乎只有val[j] - val[i]即可。但是还要再考虑一点,就是nums[i]的奇偶性。如果nums[i]是奇数,那么区间的奇数数量就是 val[j] - val[i] + 1;否则则为val[j] - val[i]。最后从头开始遍历val,对于每个val[i]来说,只要知道val[i] + k 或者val[i] + k - 1在val整个数组中出现的个数,即可求得nums以第i个元素为首的并且奇数个数为k的子数组的数量。

代码如下:

class Solution(object):
def numberOfSubarrays(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
count = 0
val = [0] * len(nums)
dic = {}
for i in range(len(nums)):
if nums[i] % 2 == 1:count += 1
val[i] = count
dic[count] = dic.setdefault(count,0) + 1
#print val res = 0 for i in range(len(nums)):
if nums[i]%2 == 0:
key = val[i] + k
else:
key = val[i] + k -1
if key in dic:
res += dic[key]
return res

【leetcode】1248. Count Number of Nice Subarrays的更多相关文章

  1. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  2. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  4. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  5. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  6. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  7. 【LeetCode】 204. Count Primes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...

  8. 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

  9. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

随机推荐

  1. gzip 命令

    NAME gzip -- compression/decompression tool using Lempel-Ziv coding (LZ77) SYNOPSIS gzip [-cdfhkLlNn ...

  2. Tableau常用图表

    条形图: 饼图: 调整大小: 折线图: 面积图: 组合图: 文本表: 突出显示表: 直方图: 气泡图: 散点图:

  3. spring5源码分析系列(二)——spring核心容器体系结构

    首先我们来认识下IOC和DI: IOC(Inversion of Control)控制反转:控制反转,就是把原先代码里面需要实现的对象创建.依赖的代码,反转给容器来帮忙实现.所以需要创建一个容器,并且 ...

  4. Linux 概念与快捷方式

    概念 何为shell Shell 是指"提供给使用者使用界面"的软件(命令解析器),类似于 DOS 下的 command(命令行)和后来的 cmd.exe .普通意义上的 Shel ...

  5. [Python3] 034 函数式编程 匿名函数

    目录 函数式编程 Functional Programming 1. 简介 2. 函数 3. 匿名函数 3.1 lambda 表达式也称"匿名函数" 3.2 lambda 表达式的 ...

  6. Redundant Connection

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  7. python-day38(正式学习)

    目录 线程 线程开启的两种方式 1 2 子线程和子进程的创建速度 子线程共享资源 线程的join方法 守护线程 线程其他用法 线程 线程开启的两种方式 1 from threading import ...

  8. Linux(17):Shell编程(4)

    案例1:批量生成随机字 符 文件名案例 使用for 循环在 /neo 目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串 neo创建的结果名称示例 如下: [root ...

  9. Git复习(十一)之常见命令用法

    创建版本库 git init 进入一个文件,执行该命令此时目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了. ...

  10. 03 Redis发布与订阅

    以qq群的公告,单个发布者,多个收听者为例 发布/订阅 实验 发布订阅的命令 PUBLISH channel msg 将信息 message 发送到指定的频道 channel SUBSCRIBE ch ...