Description:
Count the number of prime numbers less than a non-negative number, n.
一刷
题解:这题不用brute-force来求解。
i从2到n遍历, j从2到i遍历。ij<n, 用boolean数组,notPrime[Ij] = true
public class Solution {
public int countPrimes(int n) {
boolean[] notPrime = new boolean[n];
int count = 0;
for(int i=2; i<n; i++){
if(notPrime[i] == false){
count++;
for(int j=2; i*j<n; j++){
notPrime[i*j] = true;
}
}
}
return count;
}
}