204. 计数质数
代码:
class Solution {
public int countPrimes(int n) {
boolean isPrime[] = new boolean[n + 1];
for (int i = 1; i <=n; i++) isPrime[i] = true;
if (n <= 1) return 0;
if (n == 2) return 1;
int count = 1;
for (int i = 2; i < n; i++)
{
if (isPrime[i]) {
count++;
for (int j = 2; i * j < n; j++) isPrime[i * j] = false;
}
}
return count;
}
}