Description:
Count the number of prime numbers less than a non-negative number, n
class Solution {
public:
int countPrimes(int n) {
bool *a = new bool[n];
for(int i = 2; i*i < n; i++)
{
if(!a[i])
{
for(int j = i; i*j < n; j++)
{
a[i*j] = true;
}
}
}
int c = 0;
for(int i = 2; i < n; i++)
{
if(a[i] == false)
c++;
}
return c;
}
};
思路:
素数不能被比它小的整数整除,建一个bool数组,从2开始, 把其倍数小于N的都删掉。
http://blog.csdn.net/xudli/article/details/45361471