【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:
int countPrimeSetBits(int L, int R) {
int res = 0;
int bits = 0;
for(int i=L; i<=R; i++)
{
bits = countBits(i);
if(isPrime(bits) && bits!=1 ) res++;//err...
}
return res;
}
int countBits(int num)
{
int res = 0;
while(num>0)
{
if(num & 1 == 1) res++;
num >>= 1;
}
return res;
}
bool isPrime(int num)
{
for(int i=sqrt(num); i>1; i--)
{
if(num%i==0) return false;
}
return true; }
};
solution2:题目中给了数的大小范围 R <= 106 < 220,那么统计出来的非零位个数cnt只需要检测是否是20以内的质数即可,所以将20以内的质数都放入一个HashSet中,然后统计出来cnt后,直接在HashSet中查找有没有即可。
class Solution {
public:
int countPrimeSetBits(int L, int R) {
int res = 0;
unordered_set<int> primes{2, 3, 5, 7, 11, 13, 17, 19};
for(int i=L; i<=R; i++)
{
int bits = 0;
int tmp = i;
while(tmp){
if(tmp&1 == 1) bits++;
tmp >>= 1;
}
res += primes.count(bits);
}
return res;
}
};
参考
1. Leetcode_easy_762. Prime Number of Set Bits in Binary Representation;
2. Grandyang;
完
【Leetcode_easy】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:// ...
- 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 ...
- 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 ...
- [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 ...
- Leetcode 762. Prime Number of Set Bits in Binary Representation
思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...
- 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] 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] 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 ...
随机推荐
- Java实习生面试题分享
1.Java有那些基本数据类型,String是不是基本数据类型,他们有何区别. Java语言提供了八种基本类型: 六种数字类型(四个整数型,两个浮点型) 字节型byte 8位 短整型short 16位 ...
- RocketMQ的技术亮点
高性能 存储原理 零拷贝 数据结构与存储逻辑 刷盘策略 长轮询PULL RocketMQ的Consumer都是从Broker拉消息来消费,但是为了能做到实时收消息,RocketMQ使用长轮询方式,可以 ...
- 服务器nginx部署PHP项目样式不出来要注意的小问题
服务器使用nginx部署PHP项目的时候如果样式没有 出来,那么很可能 location 块里出问题了. 比如 location / { root /home/wwwroot/default/php_ ...
- [TJOI2015]弦论(第k小子串)
题意: 对于一个给定的长度为n的字符串,求出它的第k小子串. 有参数t,t为0则表示不同位置的相同子串算作一个,t为1则表示不同位置的相同子串算作多个. 题解: 首先,因为t的原因,后缀数组较难实现, ...
- learning scala Function Composition andThen
Ordering using andThen: f(x) andThen g(x) = g(f(x)) Ordering using compose: f(x) compose g(x) = f(g( ...
- hbuilder mui html vue ul li 自定义循环赋值ID
<ul class="mui-table-view mui-table-view-chevron"> <li class="mui-table-view ...
- angularJs 中controller与sever
网上找到的一个例子,感觉对于初学者理解将controller抽成服务有帮助.主要是方便理解什么时候应该来做服务. html部分 <!DOCTYPE html> <html ng-ap ...
- (转)awk 详解
出处:https://blog.51cto.com/yijiu/1358416 awk详解 awk是一款非常牛逼的报告生成工具,能够将文本格式化成显示为比较直观的结果 废话不多说,直接上例子 awk的 ...
- ICEM-缺角正方体(拓块)
原视频下载地址:https://yunpan.cn/cqFIqQWrNqzNc 访问密码 6fb4
- Entity Framework 一个表多个外键关联另外一张表的相同主键
一. 报错 异常:System.Data.Entity.Infrastructure.DbUpdateException: 更新条目时出错.有关详细信息,请参阅内部异常. ---> System ...