Leetcode 204. 计数质数

筛法求素数

class Solution {
public:
    int countPrimes(int n) {
        int ans=0;
        vector<int> A(n,1);
        for(long long i=2;i<n;++i)
            if(A[i]){
                ++ans;
                for(long long k=i*i;k<n;k+=i) A[k]=0;
            }
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/bendaai/article/details/81145595
今日推荐