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;
    }
};

 

posted @ 2015-12-16 15:32  zengzy  阅读(127)  评论(0编辑  收藏  举报
levels of contents