Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation.

(Recall that the number of set bits an integer has is the number of 1s present when written in binary. For example, 21 written in binary is 10101 which has 3 set bits. Also, 1 is not a prime.)

Example 1:

Input: L = 6, R = 10
Output: 4
Explanation:
6 -> 110 (2 set bits, 2 is prime)
7 -> 111 (3 set bits, 3 is prime)
9 -> 1001 (2 set bits , 2 is prime)
10->1010 (2 set bits , 2 is prime)

Example 2:

Input: L = 10, R = 15
Output: 5
Explanation:
10 -> 1010 (2 set bits, 2 is prime)
11 -> 1011 (3 set bits, 3 is prime)
12 -> 1100 (2 set bits, 2 is prime)
13 -> 1101 (3 set bits, 3 is prime)
14 -> 1110 (3 set bits, 3 is prime)
15 -> 1111 (4 set bits, 4 is not prime)

Note:

  1. L, R will be integers L <= R in the range [1, 10^6].
  2. R - L will be at most 10000.

Code

class Solution:
def countPrimeSetBits(self, L, R):
d = set([2,3,5,7,11,13,17,19,23,29,31])
return sum(bin(i).count('') in d for i in range(L, R+1))

[LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy的更多相关文章

  1. LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告

    题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

  2. Leetcode 762. Prime Number of Set Bits in Binary Representation

    思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...

  3. 【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation

    problem 762. Prime Number of Set Bits in Binary Representation solution1: class Solution { public: i ...

  4. 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...

  5. [LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation

    Given two integers L and R, find the count of numbers in the range [L, R](inclusive) having a prime ...

  6. 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量

    [抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

  7. 762. Prime Number of Set Bits in Binary Representation

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  8. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  9. [Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

随机推荐

  1. python 写入CSV出现空白行问题及拓展

    最近在学习python,要求让出表格.期间在不懂得情况下,写了些代码,运行后发现存在输入写入猴行之间存在空白行.猴发现原来问题在打开文件的方式不对. 现将我的学习交流经验分享如下: 1,自己的起初代码 ...

  2. okvis代码解读

    okvis_timing模块 提供时间转换的功能函数 okvis_util模块 包含 assert_macros.hpp  该文件包含一些有用的断言宏. source_file_pos.hpp 该文件 ...

  3. log4j.properties 日志文件的详细配置说明

    一.在一个web 项目中,使用tomcat 启动通常会在控制台输出出现一个警告信息: 通常为未添加 log4j.properties文件的原因. 二.下面以一个普通的maven项目为例说明一下 1. ...

  4. Python中给List添加元素的4种方法

    https://blog.csdn.net/hanshanyeyu/article/details/78839266 List 是 Python 中常用的数据类型,它一个有序集合,即其中的元素始终保持 ...

  5. quartz 2.1.6使用方法

    /** * @Description: * * @Title: QuartzManager.java * @Package com.joyce.quartz * @Copyright: Copyrig ...

  6. JS实现倒计时(天数,时,分,秒)

    <!DOCTYPE html> <html> <head>   <meta charset="utf-8" >   <titl ...

  7. 洛谷P4247 序列操作 [清华集训] 线段树

    正解:线段树 解题报告: 传送门! 通过这题我get了一个神奇的,叫,线段树五问的东西hhhh 听起来有点中二但感觉真正做题的时候还是比较有用的,,,?感觉会让条理清晰很多呢,所以放一下QwQ →每个 ...

  8. Ubuntu14.04设置开机启动脚本(转)

    原文:https://www.magentonotes.com/ubuntu-config-autostart-shell-script.html 方法一:将脚本添加到文件/etc/rc.local ...

  9. 帝国cms支持的变量及灵动标签变量汇总

    帝国CMS对首页.列表页.内容页这三个页面模板支持的变量是不同的,有的是通用的,有的不是通用的,本文就这三个模板常用的变量列于此,另外灵动标签很好用啊,也顺便收藏于此,以备后用,到时不用到处翻来翻去的 ...

  10. sql server 跟踪日志

    1,当SQL Server错误日志很大时,手工运行:  exec sp_cycle_errorlog 系统存储过程,即可使用新的日志文件 sqlserver系统自动调用 exec sp_cycle_e ...