0
点赞
收藏
分享

微信扫一扫

762. 二进制表示中质数个计算置位C++

小贴贴纸happy 2022-04-05 阅读 58
c++

链接:

762. 二进制表示中质数个计算置位.

描述,示例和提示:

在这里插入图片描述
在这里插入图片描述

代码:

class Solution {
public:
    unordered_set<int> prime = {2,3,5,7,11,13,17,19};
    int countPrimeSetBits(int left, int right) {
        int ans = 0;
        for(int i = left; i <= right; i++) {
            int cnt = __builtin_popcount (i);//__builtin_popcount统计一个数的二进制有多少个一
            if(prime.count(cnt)) ans++;
        }
        return ans;
    }
};
举报

相关推荐

0 条评论