BiruLyu
6/12/2017 - 5:21 PM

204. Count Primes.java

public class Solution {
    public int countPrimes(int n) {
        if( n <= 2) return 0;
        int count = n / 2;
        boolean[] isNotPrime = new boolean[n + 1];
        for (int i = 3; i * i < n; i += 2) {
            if(isNotPrime[i]) {
                continue;
            }
            for( int j = i; i * j < n; j += 2) {
                if(isNotPrime[i * j]) {
                    continue;
                }
                isNotPrime[i * j] = true;
                count--;
            }
        }
        return count;
    }
}