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

Example 1:

Input: L = 6, R = 10
Output: 4
Explanation:
6 -> 110 (2 set bits, 2 is prime)
7 -> 111 (3 set bits, 3 is prime)
9 -> 1001 (2 set bits , 2 is prime)
10->1010 (2 set bits , 2 is prime)

Example 2:

Input: L = 10, R = 15
Output: 5
Explanation:
10 -> 1010 (2 set bits, 2 is prime)
11 -> 1011 (3 set bits, 3 is prime)
12 -> 1100 (2 set bits, 2 is prime)
13 -> 1101 (3 set bits, 3 is prime)
14 -> 1110 (3 set bits, 3 is prime)
15 -> 1111 (4 set bits, 4 is not prime)

Note:

  1. L, R will be integers L <= R in the range [1, 10^6].
  2. R - L will be at most 10000.

这道题给了我们一个整数范围[L, R],让我们统计其中有多个整数,其二进制表示中非零位个数为质数。参考题目中的例子不难理解题意,那么博主最先想到的就是暴力搜索啊,毕竟是到Easy题嘛,可能不需要太多的技巧。我们遍历整数范围[L, R]中的每一个数字,然后先统计出所有非零位个数cnt,通过和1相与,再右移一位的方式。然后就是来判断这个cnt是否是质数,判断的方法就是就是从其平方开始,一个一个的除,如果一直到2都没有约数,那么就是质数啦,结果res累加1,参见代码如下:

解法一:

class Solution {
public:
int countPrimeSetBits(int L, int R) {
int res = ;
for (int i = L; i <= R; ++i) {
int t = i, cnt = ;
while (t > ) {
if (t & == ) ++cnt;
t >>= ;
}
bool succ = true;
for (int j = sqrt(cnt); j > ; --j) {
if (cnt % j == ) {
succ = false; break;
}
}
if (succ && cnt != ) ++res;
}
return res;
}
};

好,下面我们来优化一下上面的解法,由于题目中给了数的大小范围 R <= 106 < 220,那么我们统计出来的非零位个数cnt只需要检测是否是20以内的质数即可,所以我们将20以内的质数都放入一个HashSet中,然后统计出来cnt后,直接在HashSet中查找有没有即可,参见代码如下:

解法二:

class Solution {
public:
int countPrimeSetBits(int L, int R) {
int res = ;
unordered_set<int> primes{, , , , , , , };
for (int i = L; i <= R; ++i) {
int cnt = ;
for (int j = i; j > ; j >>= ) {
cnt += j & ;
}
res += primes.count(cnt);
}
return res;
}
};

下面这种写法就更简洁啦,直接使用了C++的内置函数__builtin_popcount来快速的求出非零位的个数cnt,然后又利用到了20以内的数,只要不能被2和3的一定是质数,又可以快速判断了质数了,参见代码如下:

解法三:

class Solution {
public:
int countPrimeSetBits(int L, int R) {
int res = ;
for (int i = L; i <= R; ++i) {
int cnt = __builtin_popcount(i);
res += cnt < ? cnt > : (cnt % && cnt % );
}
return res;
}
};

类似题目:

Number of 1 Bits

参考资料:

https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/discuss/113225/Short-C++-12-ms

https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/discuss/113227/JavaC++-Clean-Code

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数的更多相关文章

  1. Leetcode762.Prime Number of Set Bits in Binary Representation二进制表示中质数个计算置位

    给定两个整数 L 和 R ,找到闭区间 [L, R] 范围内,计算置位位数为质数的整数个数. (注意,计算置位代表二进制表示中1的个数.例如 21 的二进制表示 10101 有 3 个计算置位.还有, ...

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

  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 解题报告(Python)

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

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

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

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

  8. LeetCode算法题-Prime Number of Set Bits in Binary Representation(Java实现)

    这是悦乐书的第311次更新,第332篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第180题(顺位题号是762).给定两个正整数L和R,在[L,R]范围内,计算每个整数的 ...

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

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

随机推荐

  1. C语言描述队列的实现及操作(链表实现)

    // 队列的单链表实现 // 头节点:哨兵作用,不存放数据,用来初始化队列时使队头队尾指向的地方 // 首节点:头节点后第一个节点,存放数据 #include<stdio.h> #incl ...

  2. location和location.href跳转url的区别

    使用 location = url  跳转,如果本地之前已经载入过该页面并有缓存,那么会直接读取本地的缓存,缓存机制是由本地浏览器设置决定的.状态码为:  200 OK (from cache) . ...

  3. NOIP知识点

    基础算法 贪心 枚举 分治 二分 倍增 高精度 模拟 图论 图 最短路(dijkstra.spfa.floyd) 最小生成树(kruskal.prim) 并查集 拓扑排序 二分图染色 Tarjan 树 ...

  4. django restful 1-在线Python编辑器

    客户端(浏览器)----> 前端页面-----> 后端处理数据,并把数据以 json 形式发送到前端 online_app.py from django.conf import setti ...

  5. 一个CSS简单入门网站

    讲的知识简单明了,很实用: http://zh.learnlayout.com/

  6. C#中DBNull.Value和Null的用法和区别

    DBNull.Value,, 是适用于向数据库的表中插入空值.而 null,是指在程序中表示空引用. 或者对象为空.就是没有实例化. row[column]的值为DBNull.Value的话,至少说明 ...

  7. Python内置函数(65)——staticmethod

    英文文档: staticmethod(function) Return a static method for function. A static method does not receive a ...

  8. Docker学习实践 - Docker安装MySql数据库

    Docker安装MySQL数据库 1.Ubuntu安装MySQL安装 (1)安装编译源码需要的包 sudo apt-get install make cmake gcc g++ bison libnc ...

  9. 新概念英语(1-25)Mrs. Smith's Kitchen

    What colour is the electric cooker? Mrs. Smith's Kitchen is small. There is a refrigerator in the ki ...

  10. Jetty入门(1-2)eclipse集成jetty插件并发布运行应用

    一.eclipse集成jetty插件 1.从市场安装jetty插件 2.使用jetty插件发布应用和配置运行环境 debug配置默认共用上述run配置 3.使用jetty插件启动运行和停止运行选中的应 ...