您的位置:首页 > 其它

LeetCode—Count Primes

2015-07-20 15:31 281 查看
Description:

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

就是求小于非负整数n的质素的个数(质数:只能被自己和1整除的整数)。

public int countPrimes(int n) {
if(n==0)
return 0;
if(n==1)
return 0;
int count=0;
boolean[] notPrime=new boolean
;
for(int i=2;i<n;i++){
if(notPrime[i]==false)
count++;
for(int j=2;i*j<n;j++){
notPrime[i*j]=true;
}
}
return count;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: