参考了官网hint
代码如下:
class Solution {
public:
int countPrimes(int n) {
bool *arr = new bool[n + 1];
for(int i = 2; i < n; i++)
arr[i] = true;
for(int i = 2; i * i < n; i++) {
for(int j = i * i; j < n; j = j + i) {
arr[j] = false;
}
}
int counter = 0;
for(int i = 2; i < n; i++) {
if(arr[i])
counter++;
}
return counter;
}
};