Count the number of prime numbers less than a non-negative number, n.
class Solution {
public:
int countPrimes(int n) {
bool isPrime[n];
int result = 0;
memset(isPrime, 1, sizeof(isPrime));
for(int i = 2; i*i < n; i++)
{
if(!isPrime[i])
continue;
for(int j = i*i; j < n; j += i)
{
isPrime[j] = false;
}
}
for(int i = 2; i < n; i++)
{
if(isPrime[i])
result++;
}
return result;
}
};