public class Solution {
public int countPrimes(int n) {
boolean[] isPrimes = new boolean[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 count = 0;
for (int i = 2; i < n; i++) {
if (isPrimes[i]) {
count++;
}
}
return count;
}
}
Count Primes
最新推荐文章于 2021-02-13 14:09:32 发布