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;
}
}