Count Primes

Total Accepted: 43534 Total Submissions: 196140 Difficulty: Easy

Description:

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

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

        return count;
    }
};
原文地址:https://www.cnblogs.com/zengzy/p/5051336.html