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

  

posted @ 2017-09-28 23:10  apanda009  阅读(108)  评论(0编辑  收藏  举报