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 ...
随机推荐
- 【iCore1S 双核心板_FPGA】例程十六:基于SPI的ARM与FPGA通信实验
实验现象: 核心代码: int main(void) { int i,n; ]; ]; HAL_Init(); system_clock.initialize(); led.initialize(); ...
- 编写自定义Yeoman生成器简述
1. 安装生成器Yeoman提供了generator-generator方便快速编写自己的生成器. 安装: npm install -g generator-generator运行: yo gener ...
- Java知多少(38)抽象类的概念和使用
在自上而下的继承层次结构中,位于上层的类更具有通用性,甚至可能更加抽象.从某种角度看,祖先类更加通用,它只包含一些最基本的成员,人们只将它作为派生其他类的基类,而不会用来创建对象.甚至,你可以只给出方 ...
- 【转】VMware Tools installation cannot be started manually while Easy Install is in progress.
我HOST 是WIN7也遇到了这样的问题 我这样做的:进入系统以后在你VM的下面会有几个图标:关于1,CD DVD 2,disk 3,floppy 4,网络连接... 选择floppy--settin ...
- hosts 不启作用的解决办法
查各种编码ANSI flushdns无用 停止DNS Client 服务 0.0
- 超简单Windows安装Scrapy (仅需一步)
网上很多关于windows安装Scrapy的教程都非常的繁琐,请看我给大家分享的教程,非常简单 一步完成. 超简单的安装方法: 下载地址: https://www.continuum.io/downl ...
- Java面试题考点全面总结
我通过两个月的复习拿到了阿里巴巴的 offer,有一些运气,也有一些心得,借着跳槽季来临特此分享出来. 简单梳理一下我的复习思路,同时也希望和大家一起交流讨论,一起学习,如果不对之处欢迎指正一起学习. ...
- duilib进阶教程 -- 在duilib中使用MFC (2)
好人做到底,送佛送到西.虽然上一篇教程已经说得很详细了,但Alberl还是再举一个例子.上一篇教程的主窗口是MFC的,所以这篇教程把主窗口换成duilib的~O(∩_∩)O~ 1.我们利用<20 ...
- [DL] *Deep Learning for Industry - Wang Yi
Link: 分布式机器学习系列讲座 - 04 Deep Learning WANG Yi. https://cxwangyi.wordpress.com/ https://www.zhihu.com/ ...
- 8 -- 深入使用Spring -- 5... Spring 3.1 新增的缓存机制
8.5 Spring 3.1 新增的缓存机制 Spring 3.1 新增了一种全新的缓存机制,这种缓存机制与Spring容器无缝地整合在一起,可以对容器中的任意Bean或Bean的方法增加缓存.Spr ...