204.Count Primes(1-N中,素数的个数)

来源:互联网 发布:淘宝网首页女装春装 编辑:程序博客网 时间:2024/05/17 23:12

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


代码:

public class Solution {
    public int countPrimes(int n) {
        boolean [] a = new boolean[n];
        int count = 0;
        for(int i=2;i<n;i++){
            if(a[i]==false){
                count++;
            }
            for(int j = 2 ;i*j<n;j++){
                a[i*j]=true;
            }
        }
        
        return count ;
    }
}

0 0
原创粉丝点击