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

leetcode 204. Count Primes(线性筛素数)

时间:2016-04-25 06:46:53      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

Description:

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

题解:就是线性筛素数的模板题。

class Solution {
public:
    int countPrimes(int n) {
        int ans=0;
        vector<int>is_prime(n+1,1);
        for(int i=2;i<n;i++){
            if(is_prime[i]){
                ans++;
                for(int j=2*i;j<n;j+=i){
                    is_prime[j]=0;
                }
            }
        }
        return ans;
    }
};

 

leetcode 204. Count Primes(线性筛素数)

标签:

原文地址:http://www.cnblogs.com/zywscq/p/5429212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!