码迷,mamicode.com
首页 > 其他好文 > 详细

【Leetcode】Count Primes

时间:2016-06-02 13:49:18      阅读:132      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!