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:

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

Example 2:

  1. Input: L = 10, R = 15
  2. Output: 5
  3. Explanation:
  4. 10 -> 1010 (2 set bits, 2 is prime)
  5. 11 -> 1011 (3 set bits, 3 is prime)
  6. 12 -> 1100 (2 set bits, 2 is prime)
  7. 13 -> 1101 (3 set bits, 3 is prime)
  8. 14 -> 1110 (3 set bits, 3 is prime)
  9. 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.
  1. class Solution:
  2. def countPrimeSetBits(self, L, R):
  3. """
  4. :type L: int
  5. :type R: int
  6. :rtype: int
  7. """
  8. def is_prime(a):
  9. if a<=1:
  10. return False
  11. i=2
  12. while i*i<=a:
  13. if a%i==0:
  14. return False
  15. i+=1
  16. return True
  17.  
  18. ans=0
  19. for i in range(L,R+1):
  20. b=bin(i)[2:]
  21. if is_prime(b.count('1')):
  22. ans+=1
  23.  
  24. return ans

  

[LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation的更多相关文章

  1. 【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 ...

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

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

  3. 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 ...

  4. 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 ...

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

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

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

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

  7. [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy

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

  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. Linux安装配置samba教程(CentOS 6.5)

    一.服务端安装配置samba 1.1 服务端安装samba yum install -y samba 1.2 创建共享目录并写入配置文件 以/samba为共享目录为例,为了更直观地观测我们在该目录中创 ...

  2. JavaScript应用于asp开发场景

    JavaScript应用于asp开发场景 演示代码示例: <%Path="../"%> <!--#include file="../../Inc/Con ...

  3. samba共享文件夹设置

    sudo apt-get install samba(4) mkdir /home/用户名/share (新建share文件夹) sudo cp /etc/samba/smb.conf /etc/sa ...

  4. zabbix3.4.7安装在centos 7.4上

    Centos 7.4 安装Zabbix 3.4 一.安装环境 1 [root@juny-18 ~]# cat /etc/redhat-release 2 3 CentOS Linux release ...

  5. 隔行变色&&鼠标移入变色

    <html lang="en"> <head> <meta charset="UTF-8"> <title>Do ...

  6. linux初始化宏__init, __exit

    我们在内核中经常遇到初始化函数是这样定义的:static int __init init_func(); ,与普通函数相比,定义中多了__init.那么,__init是什么意思呢?还有与其匹配的__e ...

  7. am335x system upgrade uboot ethernet(二)

    系统可以通过SD卡引道之后,为了之后了调试方便 通过查看网卡的硬件设计 正常来说需要注意的有如下几点: 1) 网口 的接线方式: RMII 2) 网口的PHY地址两张网口,这里我们只需先初始化一张网卡 ...

  8. Linux查看某个进程的磁盘IO读写情况 pidstat

    一.现象 1)钉钉告警不断,告警如下CPU使用达到100% 普罗米修斯监控 2)查看数据库,没有发现比平时同一时段,业务量的增加.但是,数据库显示latch free等告警,验证了CPU使用过高导致. ...

  9. angular4-自定义组件

    在 Angular 中,我们可以使用 {{}} 插值语法实现数据绑定. 新建组件 $ ng generate component simple-form --inline-template --inl ...

  10. 一: Docker的概念

    附件:https://files.cnblogs.com/files/chaos-li/docker-k8s-devops-master-9287a2ca56433ca076078b564de9488 ...