题目要求

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. 聊聊Python中的闭包和装饰器

    1. 闭包 首先我们明确一下函数的引用,如下所示: def test1(): print("--- in test1 func----") # 调用函数 test1() # 引用函 ...

  2. Caffe、TensorFlow、MXnet三个开源库对比+主流分类模型对比

    库名称 开发语言 支持接口 安装难度(ubuntu) 文档风格 示例 支持模型 上手难易 Caffe c++/cuda c++/python/matlab *** * *** CNN ** MXNet ...

  3. svn-checkout后,循环遍历查找包含某字符串的文件

    这里涉及几个知识点: 1.安装subversion,不多说了,网上有教程 2.循环遍历所有目录层级,找相 关文件 #!/bin/bash #########svn checkout项目出来 svn_d ...

  4. golang IO 流抽象与应用

    https://blog.csdn.net/pmlpml/article/details/82930191

  5. SpringBoot------全局异常捕获和自定义异常

    1.添加Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...

  6. MassTransit入门

    .NET平台ESB框架的中文资料少的可怜,NServiceBus的有几篇,MassTransit的根本找不到,只好硬着头皮看官方的英文文档,顺便翻译出来加深理解. 欢迎拍砖. MassTransit是 ...

  7. a or an

    在英语句子中用a还是用an,一直是个容易出错的问题. 原则为:如果下一个词的发音为元音则用an,否则用a. 例: a man an elephant a house an hour 对于大写字母要注意 ...

  8. node.js 简单的获取命令参数

    class Argvs { constructor() { this.argvsAll = this.argvsAll(); } argvsAll() { return process.argv.sl ...

  9. thinkphp或thinkcmf 《文章编辑,文章添加》 访问另一个表的分类,添加入另一个表时将id值以(,)逗号分隔储存,编辑时以(,)逗号分隔并且相等的id值被选中

      首页 显示 的控制器//网贷评级 public function grade(){ $archives = $this->archives_model->where(array('de ...

  10. Vue----目录结构

    目录结构: (1):build:---------------------------------------------------------------------------------:保存 ...