leetcode 204 Count Primes

class Solution {
public:
    int countPrimes(int n) {
        if(!n) return 0;
        vector<bool> isPrime(n,true);
        isPrime[0]=false;isPrime[1]=false;
        for(int i=2;i*i<n;++i) {
            if(!isPrime[i]) continue;
            for(int j=i*i;j<n;j+=i) isPrime[j]=false;
        }
        int re=0;
        for(int i=1;i<n;++i) {
            if(isPrime[i]) ++re;
        }
        return re;
    }
};

 

posted @ 2020-03-26 10:40  qiujiejie  阅读(117)  评论(0编辑  收藏  举报