leetcode 762. Prime Number of Set Bits in Binary Representation

#include<cmath>
class Solution {
public:
    bool isPrime(int l)
    {
        if(l==0||l==1)return false;
        if(l==2||l==3)return true;
        for(int i=2;i<=sqrt(l);i++)
        {
            if(l%i==0)return false;
        }
        return true;
    }
    int countPrimeSetBits(int L, int R) {
        int sum=0;
        for(int i=L;i<=R;i++)
        {
            int count=0;
            int j=i;
            while(j)
            {
                if(j%2==1)count++;
                 j = j/2;
            }
            
            if(isPrime(count))
            {
               // cout<<count<<endl;
                sum++;
            }
        }
        return sum;
    }
};

猜你喜欢

转载自blog.csdn.net/wangchy29/article/details/89262298