Description:
Count the number of prime numbers less than a non-negative number, n.
解法一:
大神的解法只能强记了。。。
class Solution {
public:
int countPrimes(int n) {
bool isPrimes[n];
for(int i=2; i<n; ++i)
isPrimes[i] = true;
for(int i =2; i*i<n; ++i){
if (!isPrimes[i]) continue;
for(int j = i*i; j<n; j+=i)
isPrimes[j] = false;
}
int res = 0;
for(int i=2; i<n; ++i){
if (isPrimes[i]) res++;
}
return res;
}
};