首页 > 其他 > 详细

【Leetcode】Count Primes

时间:2016-06-02 13:47:06      阅读:197      评论:0      收藏:0      [点我收藏+]

题目链接:https://leetcode.com/problems/count-primes/

题目:

Description:

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

思路:

埃拉托色尼选筛法

算法:

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


【Leetcode】Count Primes

原文:http://blog.csdn.net/yeqiuzs/article/details/51558614

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