给定两个整数 L 和 R ,找到闭区间 [L, R] 范围内,计算置位位数为质数的整数个数。

(注意,计算置位代表二进制表示中1的个数。例如 21 的二进制表示 10101 有 3 个计算置位。还有,1 不是质数。)

示例 1:

输入: L = 6, R = 10 输出: 4 解释: 6 -> 110 (2 个计算置位,2 是质数) 7 -> 111 (3 个计算置位,3 是质数) 9 -> 1001 (2 个计算置位,2 是质数) 10-> 1010 (2 个计算置位,2 是质数)

示例 2:

输入: L = 10, R = 15 输出: 5 解释: 10 -> 1010 (2 个计算置位, 2 是质数) 11 -> 1011 (3 个计算置位, 3 是质数) 12 -> 1100 (2 个计算置位, 2 是质数) 13 -> 1101 (3 个计算置位, 3 是质数) 14 -> 1110 (3 个计算置位, 3 是质数) 15 -> 1111 (4 个计算置位, 4 不是质数)

注意:

  1. L, R 是 L <= R 且在 [1, 10^6] 中的整数。
  2. R - L 的最大值为 10000。
class Solution {
public:
int countPrimeSetBits(int L, int R)
{
vector<int> prim = GetPrim();
int len = prim.size();
map<int ,int> check;
for(int i = 0; i < len; i++)
{
check[prim[i]] = 1;
}
int res = 0;
for(int i = L; i <= R; i++)
{
int temp = i;
int x = 0;
while(temp)
{
if(temp & 1 == 1)
x++;
temp >>= 1;
}
if(check[x] == 1)
res++;
}
return res;
} vector<int> GetPrim()
{
int len = 64;
vector<int> check(65, 0);
vector<int> res;
check[0] = 1;
check[1] = 1;
for(int i = 2; i <= 64; i++)
{
if(check[i] == 1)
continue;
for(int j = i + i; j <= 64; j += i)
{
check[j] = 1;
}
}
for(int i = 1; i <= 64; i++)
{
if(check[i] != 1)
res.push_back(i);
}
return res;
}
};

Leetcode762.Prime Number of Set Bits in Binary Representation二进制表示中质数个计算置位的更多相关文章

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

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

  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. 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)

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

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

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

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

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

随机推荐

  1. Ubuntu18上安装Go和GoLand

    第一步骤:安装Go 方式一: 使用 sudo apt-get install golang命令安装 ubuntu软件库里当前golang版本为1.10,(golang最新版为1.11),可满足要求. ...

  2. Odoo中使用的部分表名及用途

    res_users 用户res_groups 用户组(角色)res_lang 语言res_partner 供应商/客户/联系人res_font 字体res_company 公司res_bank 银行r ...

  3. leetcode 49 Group Anagram

    lc49 Group Anagram 逻辑很简单,就是统计字母出现次数,然后将完全相同的字符串放入同一list 关键是怎么实现 统计的部分,可以通过将string排序,Arrays.sort(),或者 ...

  4. Ionic 分享功能(微博 微信 QQ)

    1.安装插件 cordova plugin add cordova-plugin-wechat --variable wechatappid=你申请微信appid cordova plugin add ...

  5. 你真的了解ES6的promise吗?

    promise是一个构造函数,是用来解决ajax回调地狱的问题.axios就是用promise封装的.用于解决ajax请求时出现的回调地狱的问题.异步伴随回调. const p1 = new Prom ...

  6. Java内功修炼系列一观察者模式

    观察者模式又称发布-订阅模式,就是观察者通过订阅被观察者,或关注被观察者,从而实时更新观察者的信息.比如我们玩微博的时候,如果关注了一些博主,那么当博主发动态时,在首页微博列表中就会自动更新这些博主发 ...

  7. Google 和 微软,就不是一个重量级的

    看微软的 WinCE 模拟器,做得带模带样   Google 也不是新来的,但是 Android 模拟器,配置一下都那么费事   这就是差距,Google 比 微软 差了好几级

  8. JS---案例:无刷新评论---属于创建对象的案例拿出来复习

    案例:无刷新评论---属于创建对象的案例拿出来复习 创建行和单元格,添加到相应元素中,设置内容 createElement, appendChild,innerHTML <!DOCTYPE ht ...

  9. 深入浅析python中的多进程、多线程、协程

    深入浅析python中的多进程.多线程.协程 我们都知道计算机是由硬件和软件组成的.硬件中的CPU是计算机的核心,它承担计算机的所有任务. 操作系统是运行在硬件之上的软件,是计算机的管理者,它负责资源 ...

  10. Java 类与类之间的调用

    方法1. 新建一个类. 然后在调用类中先进行被调用类实例化,然后通过实例化的对象访问. 例如: //先定义一个类 import static java.lang.System.out; public ...