204.Count Primes(1-N中,素數的個數)

題目: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 ;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章