思路:
References给出了参考的方法:埃拉托斯特尼筛法。
时间复杂度O(N)。
class Solution {
public:
int countPrimes(int n) {
int count = 0;
vector<bool> map(n+1,false);
for(int i = 2; i < n; ++i) {
if(map[i] == false) {
count++;
for(int j = i*2; j < n; j+=i) map[j] = true;
}
}
return count;
}
};