Description:

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

刷了一周的python, 希望明天把blackjack和最后的游戏做出来。

思路:

比较巧妙,用inner loop从头开始mark不是prime的位置,然后输出false的个数。

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

    
    return count;
}
}

 

posted on 2016-10-08 17:14  Machelsky  阅读(91)  评论(0)    收藏  举报