力扣练习之计数质数

我爱海鲸 2022-10-10 18:34:10 初级算法

简介初级算法、数学

原题出处:https://leetcode.cn/leetbook/read/top-interview-questions-easy/xnzlu6/

解法一:

class Solution {
    public int countPrimes(int n) {
        boolean[] bool = new boolean[n];
        int result = 0;
        for (int i = 2; i < n; i++) {
            if (bool[i]) {
                continue;
            }
            result ++;
            for (int j = i ; j < n; j += i) {
                bool[j] = true;
            }
        }
        return result;
    }
}

思路:埃氏筛

枚举 < 埃氏筛 < 欧式筛(线性筛) < 奇数筛

你好:我的2025