Description:
Count the number of prime numbers less than a non-negative number, n.
方法:利用哈希查表法进行排除。
class Solution {
public:
int countPrimes(int n) {
vector<bool> vt(n,true);
for(int i = 2; i * i <n; ++i){
if(!vt[i])
continue;
for(int j = i * i; j < n;j+=i){
vt[j]=false;
}
}
int count = 0;
for(int i = 2 ;i <n;++i )
count += vt[i];
return count;
}
};