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   gechen  阅读(61)  评论(0编辑  收藏  举报

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

导航

点击右上角即可分享
微信分享提示