/*根据提示求解。*/
class Solution {
public:
int countPrimes(int n) {
bool mark[n];
memset(mark, 1, sizeof(mark));
int sqrt_n = sqrt(n);
for(int i = 2; i <= sqrt_n; ++i){
if(mark[i]){
for(int j = i*i; j < n; j += i){
mark[j] = false;
}
}
}
int count = 0;
for(int i = 2; i < n; ++i){
if(mark[i]) ++count;
}
return count;
}
};
LeetCode之Count Primes
最新推荐文章于 2019-10-14 14:31:12 发布