`
hcx2013
  • 浏览: 88758 次
社区版块
存档分类
最新评论

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics