Leetcode762.Prime Number of Set Bits in Binary Representation二进制表示中质数个计算置位
给定两个整数 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 不是质数)
注意:
- L, R 是 L <= R 且在 [1, 10^6] 中的整数。
- 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二进制表示中质数个计算置位的更多相关文章
- [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 ...
- 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_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 ...
- [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 ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...
- Leetcode 762. Prime Number of Set Bits in Binary Representation
思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...
- LeetCode算法题-Prime Number of Set Bits in Binary Representation(Java实现)
这是悦乐书的第311次更新,第332篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第180题(顺位题号是762).给定两个正整数L和R,在[L,R]范围内,计算每个整数的 ...
随机推荐
- leyou_06_图片上传至FastDFS
1.推荐一个开源的FastDFS客户端,支持最新的SpringBoot2.0.配置使用极为简单,支持连接池,支持自动生成缩略图 1.1 在文件上传的微服务中 引入依赖 <dependency&g ...
- Java 普通代码块,构造代码块,静态代码块
具体百度... 这里只记下,构造构造代码块在构造函数之前执行,每创建一个对象则执行一次. 静态代码块属于类,而不是对象,只执行一次.
- crontab定时任务语法及应用
https://mp.weixin.qq.com/s/Oi9hppNQMeFiQo9s-ge79A crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows ...
- 软件-UlitraEdit:UIitraEdit
ylbtech-软件-UlitraEdit:UIitraEdit UltraEdit 是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII 码,完全可以取代记事本(如果电脑配置足够强大),内 ...
- Session - 什么叫一次会话
转载自:https://blog.csdn.net/qin_xiaofang/article/details/77725946 网上收集的:Session代表服务器与浏览器的一次会话过程,这个过程是连 ...
- GC Roots的几种对象
在java语言里,可作为GC Roots的对象包括下面几种: >虚拟机栈(栈帧中的本地变量表)中的引用的对象: >方法区中类静态属性引用的对象: >方法区中常量引用的对象: > ...
- JobTracker与TaskTracker的关系
JobTracker 对应于 NameNode TaskTracker 对应于 DataNode DataNode 和NameNode 是针对数据存放来而言的 JobTracker和TaskTrac ...
- PHP1.6--数组
一.数组的键值操作函数 1.array_values() 函数作用是返回数组中所有元素的值,只有一个参数,规定传人给定数组,返回一个包含给定数组中所有值的数组,但不保留键名 被返回的数组将使用顺序的数 ...
- 记一次PHP 数组基本用法
以前不知道PHP数组可以这样叠加. $b = array( '2' => 'zhang', ); $a = array( ' => 'li' ) + $b; print_r($a); $b ...
- ACdream 1108(莫队)
题目链接 The kth number Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) ...