首页 > 其他 > 详细

204. Count Primes

时间:2016-10-08 19:20:37      阅读:247      评论:0      收藏:0      [点我收藏+]

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

 

204. Count Primes

原文:http://www.cnblogs.com/Machelsky/p/5939450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!