204. Count Primes
Description:
Count the number of prime numbers less than a non-negative number, n.
动归来做,
public int countPrimes(int n) { boolean[] notPrime = new boolean[n]; int ans = 0; if (n < 2) { return ans; } for (int i = 2; i < n; i++) { if (!notPrime[i]) { ans++; for (int j = 2; i * j < n; j++) { notPrime[i * j] = true; } } } return ans; }