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. Sweet Butter 香甜的黄油

    Sweet Butter 香甜的黄油 题目大意:m个点,n头奶牛,p条边,每一头奶牛在一个点上,一个点可以有多只奶牛,求这样一个点,使得所有奶牛到这个点的距离之和最小. 注释:n<=500 , ...

  2. markdown语法小结

    引用数学公式1 \[ \begin{equation} \pi^2=x^2+y \label{eq_lab1} \end{equation} \] Here we cite this equation ...

  3. Linux下C编写基本的多线程socket服务器

    不想多说什么,会搜这些东西的都是想看代码的吧. 一开始不熟悉多线程的时候还在想怎么来控制一个线程的结束,后来发现原来有pthread_exit()函数可以直接在线程函数内部调用结束这个线程. 开始还想 ...

  4. Beta预备

    团队名称:稳住!我们能赢 Beta预备: 讨论组长是否重选的议题和结论 项目组长可以说是一个团队的灵魂和核心.一个好的领导者可以激发团队成员的工作热情,提高开发效率,保质保量的完成工作.虽然在Alph ...

  5. iOS开发-添加圆角效果高效实现

    圆角(RounderCorner)是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.但很多人并不清楚如何设置圆角的正确方式和原理.设置圆角会带来一定的性能损耗,如何提高性能是另一个需要重点 ...

  6. PHP、Java、Python、C、C++ 这几种编程语言都各有什么特点或优点

    PHP.Java.Python.C.C++ 这几种编程语言都各有什么特点或优点 汇编: C: Java: C#: PHP: Python: Go: Haskell: Lisp: C++: &l ...

  7. nyoj水池数目

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  8. C# 封装miniblink 使用HTML/CSS/JS来构建.Net 应用程序界面和简易浏览器

    MiniBlink的作者是 龙泉寺扫地僧 miniblink是什么?   (抄了一下 龙泉寺扫地僧 写的简洁) Miniblink是一个全新的.追求极致小巧的浏览器内核项目,其基于chromium最新 ...

  9. LeetCode & 118-Pascal's Triangle-Easy

    Array Description: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...

  10. IntelliJ IDEA sass环境配置及常见报错处理

    1.下载安装ruby,网上教程很多的,安装完之后在命令行输入ruby -v检查一下是否安装成功了.(注意安装的时候要勾选第二项).