题目要求

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 解题报告的更多相关文章

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

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

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

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

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

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

  5. [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 ...

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

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

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

  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. Brainfuck解析器(Python)

    global cs global ip global ss #global sp global ds global bp global tab global out cs='++++++++++[&g ...

  2. JavaScript高级用法一之事件响应与网页交互

    综述 本篇的主要内容来自慕课网,事件响应与网页交互,主要内容如下 1 什么是事件 2 鼠标单击事件( onclick ) 3 鼠标经过事件(onmouseover) 4 鼠标移开事件(onmouseo ...

  3. Zookeeper系列三:Zookeeper客户端的使用(Zookeeper原生API如何进行调用、ZKClient、Curator)和Zookeeper会话

    一.Zookeeper原生API如何进行调用 准备工作: 首先在新建一个maven项目ZK-Demo,然后在pom.xml里面引入zk的依赖 <dependency> <groupI ...

  4. hello alibaba

    http://ifeve.com/dubbo-learn-book/ http://ifeve.com/leader-follower-thread-model/ http://ifeve.com/a ...

  5. Cisco NTP配置

    Windows 或 Linux 系统配置成NTP服务器,思科交换机配置成NTP客户端 ##创建VLAN 10 SW01>enable SW01#vlan database SW01(vlan)# ...

  6. 场景切换 异步加载 loading条做法

    AsyncOperation mAsync; //需要加载的场景    public UISlider LoadingSlider; //NGUI做的    public UILabel GameTi ...

  7. PHP利用ImageMagick把PDF转成PNG

    http://blog.csdn.net/longaction2012/article/details/12257867

  8. MFC窗口阴影

    SetClassLong(this->m_hWnd, GCL_STYLE, GetClassLong(this->m_hWnd, GCL_STYLE) | CS_DROPSHADOW);

  9. Django详解之models操作

    D jango 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等数据库,只需要在settings.p ...

  10. Windows Server 2008 R2远程协助选项 灰色

    管理工具——〉服务器管理器——〉功能——〉添加功能   窗口中"远程服务器管理工具"下边的"远程协助"打上对钩"再点击"下一步"再 ...