/*根據提示求解。*/
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.