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的更多相关文章

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

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

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

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

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

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

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

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

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

  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. tslint.json的配置项说明

    tslint.json的配置项说明   extends: 内设配置项名称 rules: 规则 { //ts专用 adjacent-overload-signatures : true, // Enfo ...

  2. Redis vs kafka

    kafka的订阅可以重复消费,但redis的不行,只能收到订阅之后发布的数据 redis无法实现消息堆积和回溯 1 Redis是个数据库,可以改.查,而KFAKA 不能改查2 redis是内存数据库, ...

  3. Django REST framework+Vue 打造生鲜电商项目(笔记十一)

    (form: http://www.cnblogs.com/derek1184405959/p/8886796.html 有修改) 十四.social_django 集成第三方登录 1.申请应用 进入 ...

  4. ServletRequest、 HttpServletRequest、Request的联系与区别

    一. servlet理论上可以处理多种形式的请求响应形式 http只是其中之一 所以HttpServletRequest HttpServletResponse分别是ServletRequest和Se ...

  5. Vue 获取dom元素中的自定义属性值

    方法一: HTML <div id="app"> <button @click="getData($event,'100')">点我&l ...

  6. 题解 [51nod1340]地铁环线

    题解 [51nod1340]地铁环线 题面 解析 本文参考这篇博客 一开始看到只有120行就打算写一写, 结果一刚就是三个星期摆摆摆 本来是当查分约束入门学的. step 1 首先来考虑下如果已知总长 ...

  7. P3588 [POI2015]PUS

    好题 思路:线段树优化建图+拓扑DP or 差分约束(都差不多): 提交:3次 错因:眼瞎没看题,Inf写的0x3f3f3f3f 题解: 类似差分约束的模型,\(a<b\rightarrow a ...

  8. 二十八. Ceph概述 部署Ceph集群 Ceph块存储

    client   :192.168.4.10 node1 :192.168.4.11 ndoe2 :192.168.4.12 node3 :192.168.4.13   1.实验环境 准备四台KVM虚 ...

  9. leetcode解题报告(15):Third Maximum Number

    描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...

  10. 爬虫(十四):scrapy下载中间件

    下载器中间件是介于Scrapy的request/response处理的钩子框架,是用于全局修改Scrapy request和response的一个轻量.底层的系统. 激活Downloader Midd ...