当前位置:网站首页>762. 二进制表示中质数个计算置位
762. 二进制表示中质数个计算置位
2022-07-25 23:21:00 【Mr Gao】
762. 二进制表示中质数个计算置位
给你两个整数 left 和 right ,在闭区间 [left, right] 范围内,统计并返回 计算置位位数为质数 的整数个数。
计算置位位数 就是二进制表示中 1 的个数。
例如, 21 的二进制表示 10101 有 3 个计算置位。
示例 1:
输入:left = 6, right = 10
输出:4
解释:
6 -> 110 (2 个计算置位,2 是质数)
7 -> 111 (3 个计算置位,3 是质数)
9 -> 1001 (2 个计算置位,2 是质数)
10-> 1010 (2 个计算置位,2 是质数)
共计 4 个计算置位为质数的数字。
示例 2:
输入:left = 10, right = 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 不是质数)
共计 5 个计算置位为质数的数字。
这题使用常规方法去做就可以了,写两个函数就行啦,解题代码如下:
int f(int n){
int count=0;
while(n){
if(n%2==1){
count++;
}
n=n/2;
}
return count;
}
bool f2(int count){
if(count<=3&&count>1){
return true;
}
if(count<=1){
return false;
}
for(int i=2;i<=count/2;i++){
if(count%i==0){
return false;
}
}
return true;
}
int countPrimeSetBits(int left, int right){
int count=0;
for(int i=left;i<=right;i++){
if(f2(f(i))){
count++;
}
}
return count;
}
边栏推荐
猜你喜欢

通用分页功能

Data broker understanding

Single model common sense reasoning first surpasses human beings! HFL summit openbookqa challenge

XxE & XML external entity injection utilization and bypass

【接口性能优化】索引失效的原因以及如何进行SQL优化

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

E-commerce RPA, a magic weapon to promote easy entry

Zero crossing position search of discrete data (array)

类和对象(2)(6个默认成员函数)

Npm+ module loading mechanism
随机推荐
Summary of built-in instructions and custom instructions
Single model common sense reasoning first surpasses human beings! HFL summit openbookqa challenge
Mongodb update operator (modifier)
2021-09-30
VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
Simulate and implement common interfaces of string class
The fifth article in the series of radar Fundamentals: the function of radar modulation style
电商RPA,大促轻松上阵的法宝
About priority queues
serialization and deserialization
POI特效 市场调研
Details of notification (status bar notification)
Redis过期键的删除策略[通俗易懂]
自定义mvc原理
Summary of common PHP functions
Thinkphp6 temporarily close the layout
How to set pseudo static for WordPress fixed links
网格参数化Least Squares Conformal Maps实现(3D网格映射到2D平面)
Drive board network cable directly connected to computer shared network configuration
Vscode shortcut key: collapse and expand code