762. Prime Number of Set Bits in Binary Representation

 static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int countPrimeSetBits(int L, int R)
{
int res=;
for(int k=L;k<=R;k++)
{
int cur=countone(k);
if(judgeprime(cur))
res++;
}
return res;
} int countone(int i)
{
int count=;
while(i)
{
count++;
i&=i-;
}
return count;
} bool judgeprime(int i)
{
if(i==)
return false;
int last=sqrt(i);
for(int j=;j<=last;j++)
{
if(i%j==)
return false;
}
return true;
}
};

三个函数,一个统计二进制1的个数,一个判定质数,一个统计结果

上一篇:【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation


下一篇:[LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数