Count Primes

Count the number of prime numbers less than a non-negative number, n.

 

public class Solution {
    public int countPrimes(int n) {
    	boolean[] flag = new boolean[n];
    	int res = 0;
        for (int i = 2; i < n; i++) {
			if (flag[i]) {
				continue;
			}
			res++;
			for (int j = i; j < n; j += i) {
				flag[j] = true;
			}
		}
        return res;
    }
}

 

你可能感兴趣的:(Count Primes)