204. 计数质数 筛法求素数

class Solution {
public:
    int countPrimes(int n) {
        int ans = 0;
        vector <bool> is(n, true);
        for (int i = 2; i < n; i++) {
            if (is[i]) {
                ans++;
                for (int j = i + i; j < n; j += i) {
                    is[j] = false;
                }
            }
        }
        return ans;
    }
};
posted @ 2020-12-03 08:43  _西瓜不甜  阅读(50)  评论(0编辑  收藏  举报