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. Java并发包--ConcurrentHashMap原理解析

    ConcurrentHashMap实现原理及源码分析   ConcurrentHashMap是Java并发包中提供的一个线程安全且高效的HashMap实现(若对HashMap的实现原理还不甚了解,可参 ...

  2. mnist数据的预测结果以及批量处理

    import sys, os sys.path.append('F:\ml\DL\source-code') from dataset.mnist import load_mnist from PIL ...

  3. E:nth-last-child(n)

    E:nth-last-child(n) 语法: E:nth-last-child(n) { sRules } 说明: 匹配父元素的倒数第n个子元素E,假设该子元素不是E,则选择符无效.大理石平台维修 ...

  4. sparkStreaming 读kafka的数据

    目标:sparkStreaming每2s中读取一次kafka中的数据,进行单词计数. topic:topic1 broker list:192.168.1.126:9092,192.168.1.127 ...

  5. 一些VMware vCenter Appliance的默认用户名和密码

    一些VMware vCenter Appliance的默认用户名和密码 2014-03-30 17:30:03 flowershade_21 阅读数 13367更多 分类专栏: vmware   VM ...

  6. Oracle 绑定变量窥视

    绑定变量窥视功能是数据库的一个特性,自ORACLE9i版本开始引入,默认是开启的. “绑定变量窥视”表示,查询优化器在第一次调用游标时,会观察用户定义的绑定变量的值,允许优化器来确认过滤条件的选择性, ...

  7. MySQL 优化--持续整理

    一.innodb体系结构优化: 1.IO优化 IO能力不足时 innodb_io_capacity 应该降低 innodb_max_dirty_pages_pct 应该降低 innodb_max_di ...

  8. 超过20g的文件+上传

    demo下载地址:jsp-Eclipse,jsp-MyEclipse,PHP,ASP.NET 教程:ASP.NET,JSP,PHP 一. 功能性需求与非功能性需求 要求操作便利,一次选择多个文件和文件 ...

  9. string字符串类型用scanf读入,printf输出

    #include <iostream> #include <stdio.h> #include <string.h> using namespace std; in ...

  10. Educational Codeforces Round 69

    目录 Contest Info Solutions A. DIY Wooden Ladder B. Pillars C. Array Splitting D. Yet Another Subarray ...