LeetCode—762. Prime Number of Set Bits in Binary Representation


分两步,首先获取为1的bit位的数量,然后判断改数量是否是质数,
10^6的位数有限,可以先写出对应范围内的质数,加快质数的判断

//url:https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/description/
class Solution {
public:
    int getSetBitNum(int n){
        int num=0;
        while(n>0){
            num++;
            n=n&(n-1);
        }
        return num;
    }
    bool isPrime(int n){
        if(0==n||1==n)
            return false;
        int i=2;
        while(i

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注