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 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.)
题目分析及思路
给定两个整数,找到这个区间内(包括边界值)符合要求的数字个数。要求是这个数字的二进制表示中的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 ...
随机推荐
- 【转】Django中使用POST方法获取POST数据
1.获取POST中表单键值数据 如果要在django的POST方法中获取表单数据,则在客户端使用JavaScript发送POST数据前,定义post请求头中的请求数据类型: xmlhttp.setRe ...
- MySql-Binlog协议详解
Reference: https://blog.csdn.net/hj7jay/article/details/56665057?utm_source=blogxgwz7 MySql-Binlog协议 ...
- 浅析tornado 中demo的 blog模块
#!/usr/bin/env python # # Copyright 2009 Facebook # # Licensed under the Apache License, Version 2.0 ...
- left join、right join、inner join的区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...
- 【emWin】例程二十三:窗口对象——Graph
简介: 图形小工具可用于可视化数据.图形小工具的典型应用是显示测量值或函数图形的曲线,可同时显 示多条曲线.可使用水平和垂直刻度来标记曲线.可在背景上显示具有不同水平和垂直间距的网格.如 果数据阵列不 ...
- django template if return false
如果if的参数不存在于context中就会返回false 参考:http://stackoverflow.com/questions/11107028/django-template-if-true- ...
- Jquery计算指定日期加上多少天、加多少月、加多少年的日期
/* * 功能:实现VBScript的DateAdd功能. * 参数:interval,字符串表达式,表示要添加的时间间隔. * 参数:number,数值表达式,表示要添加的时间间隔的个数. * 参数 ...
- java中的数据加密5 数字证书
数字证书 A用私钥加密了,那么B接受到消息后,用A提供的公钥解密:那么现在有个讨厌的C,他把消息拦截了,然后用自己的私钥加密,同时把自己的公钥发给B,并告诉B,那是A的公钥,结果....,这时候就需要 ...
- sql server 获取动态sql输出结果
不带输出结果 我们一般会这样写 例子:一个输出6位递增号码结果 ALTER proc GetCode ), ) as declare @sqlstring nvarchar(max) set @sql ...
- 转载——githup的提交
Github是管理软件开发的首选托管网站,12306的火车票插件一时让国内当时很多小白开发者(当然也包括我)认识到了这个网站.GitHub可以托管各种git库,并提供一个web界面,与 SourceF ...