[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 number of set bits in their binary representation.
(Recall that the number of set bits an integer has is the number of 1
s 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:
L, R
will be integersL <= R
in the range[1, 10^6]
.R - L
will be at most 10000.
- class Solution:
- def countPrimeSetBits(self, L, R):
- """
- :type L: int
- :type R: int
- :rtype: int
- """
- def is_prime(a):
- if a<=1:
- return False
- i=2
- while i*i<=a:
- if a%i==0:
- return False
- i+=1
- return True
- ans=0
- for i in range(L,R+1):
- b=bin(i)[2:]
- if is_prime(b.count('1')):
- ans+=1
- return ans
[LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation的更多相关文章
- 【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 ...
- 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- 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 ...
- 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 ...
- Leetcode 762. Prime Number of Set Bits in Binary Representation
思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...
- 762. Prime Number of Set Bits in Binary Representation
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- Linux安装配置samba教程(CentOS 6.5)
一.服务端安装配置samba 1.1 服务端安装samba yum install -y samba 1.2 创建共享目录并写入配置文件 以/samba为共享目录为例,为了更直观地观测我们在该目录中创 ...
- JavaScript应用于asp开发场景
JavaScript应用于asp开发场景 演示代码示例: <%Path="../"%> <!--#include file="../../Inc/Con ...
- samba共享文件夹设置
sudo apt-get install samba(4) mkdir /home/用户名/share (新建share文件夹) sudo cp /etc/samba/smb.conf /etc/sa ...
- 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 ...
- 隔行变色&&鼠标移入变色
<html lang="en"> <head> <meta charset="UTF-8"> <title>Do ...
- linux初始化宏__init, __exit
我们在内核中经常遇到初始化函数是这样定义的:static int __init init_func(); ,与普通函数相比,定义中多了__init.那么,__init是什么意思呢?还有与其匹配的__e ...
- am335x system upgrade uboot ethernet(二)
系统可以通过SD卡引道之后,为了之后了调试方便 通过查看网卡的硬件设计 正常来说需要注意的有如下几点: 1) 网口 的接线方式: RMII 2) 网口的PHY地址两张网口,这里我们只需先初始化一张网卡 ...
- Linux查看某个进程的磁盘IO读写情况 pidstat
一.现象 1)钉钉告警不断,告警如下CPU使用达到100% 普罗米修斯监控 2)查看数据库,没有发现比平时同一时段,业务量的增加.但是,数据库显示latch free等告警,验证了CPU使用过高导致. ...
- angular4-自定义组件
在 Angular 中,我们可以使用 {{}} 插值语法实现数据绑定. 新建组件 $ ng generate component simple-form --inline-template --inl ...
- 一: Docker的概念
附件:https://files.cnblogs.com/files/chaos-li/docker-k8s-devops-master-9287a2ca56433ca076078b564de9488 ...