leetcode762
Posted AsenYang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode762相关的知识,希望对你有一定的参考价值。
class Solution { public: bool IsPrime(int n) { if (n == 1) { return false; } if (n == 2 || n == 3) { return true; } for (int i = 2; i <= sqrt(n); i++) { if (n%i == 0) { return false; } } return true; } int CountOnes(int n) { int sum = 0; while (n) { int x = n & 1; if (x) { sum++; } n >>= 1; } return sum; } int countPrimeSetBits(int L, int R) { int sum = 0; for (int i = L; i <= R; i++) { int x = CountOnes(i); if (IsPrime(x)) { sum++; } } return sum; } };
以上是关于leetcode762的主要内容,如果未能解决你的问题,请参考以下文章