LeetCode :Count Primes

Description:

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

class Solution {
public:
    int countPrimes(int n) {
        int count = 0;
        int *a = new int[n]();
        if(n<2)
           return 0;
        for(int i=2;i<n;++i){
            if(a[i]==0)
            {
                count++;
                for(int j=2;i*j<n;++j){
                    a[i*j] = 1;
                }
            }
        } 
        return count;
    }
};

posted on 2017-05-03 14:26  gechen  阅读(61)  评论(0编辑  收藏  举报

导航