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 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.)
题目分析及思路
给定两个整数,找到这个区间内(包括边界值)符合要求的数字个数。要求是这个数字的二进制表示中的1的个数为奇数。求二进制形式中的1的个数可以每一位与1做与运算,然后再左移。求素数时要特别注意0和1不是素数。
python代码
class Solution:
def countPrimeSetBits(self, L: int, R: int) -> int:
count_pr_num = 0
for i in range(L, R+1):
count_bits = 0
while i:
if i & 1 != 0:
count_bits += 1
i >>= 1
count_pr = 0
for j in range(2,count_bits):
if count_bits % j == 0:
count_pr += 1
break
if count_bits == 0 or count_bits == 1:
count_pr += 1
if count_pr == 0:
count_pr_num += 1
return count_pr_num
LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告的更多相关文章
- 【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
思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...
- 【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_Easy
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [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 ...
- 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 ...
- 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] 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 ...
随机推荐
- LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用
Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...
- 【6集iCore3_ADP触摸屏驱动讲解视频】6-4 底层驱动之SDRAM读写(上)
源视频包下载地址: 链接:http://pan.baidu.com/s/1i5lzzj3 密码:bwoe 银杏科技优酷视频发布区: http://i.youku.com/gingko8
- Java两种核心机制
1.Java虚拟机 2.垃圾回收
- Go Revel - Deployment(部署)
##概览 下面列出了部署的一些方法: 1.本地构建程序并拷贝至服务器 2.在服务器上获取更新代码,然后构建并运行 3.使用`Heroku`来管理部署 使用交互式的命令行会话来进行部署 - 通常需要一种 ...
- R语言三元相图的做法
通常情况下,对于三维数据,我们会用三维图表来展示,想要从三维图表上观察出一定的规律,需要一定的空间想象力: 而三元相图,其实就是用二维平面的1个等边三角形来表征三维数据,三角形的每一条边对应1个维度, ...
- jQuery表格列宽可变,兼容firfox
本demo使用jQuery包,实现表格列宽可拖拽功能,并实现页面reset时的重新布局.使用jQuery,方便函数的调用,给要处理的表格添加id 后,直接调用$("#id").mo ...
- IOC容器特性注入第五篇:查找(Attribute)特性注入
前面几篇文章分别介绍:程序集反射查找,特性,容器,但它们之间贯穿起来,形成查找Attribute注入IOC容器,就得需要下面这个类帮忙: 1.DependencyAttributeRegistrato ...
- 使用kill -9 进程ID杀死jps中进程
- &与&问题
1.后台传到前端的url,如果有&会被前端解析成&的(直接js获取的时候),所以最好是将数值放到input中,然后再获取
- Puppet软件资源管理
1.实现的功能: 管理那些软件包被安装,那些软件包被卸载 管理软件包是否更新 要求系统配置yum源(RedHat系统).zypper源(Suse系统)等等 2.可用参数: en ...