comments | difficulty | edit_url | tags | ||||
---|---|---|---|---|---|---|---|
true |
中等 |
|
给定整数 n
,返回 所有小于非负整数 n
的质数的数量 。
示例 1:
输入:n = 10 输出:4 解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
示例 2:
输入:n = 0 输出:0
示例 3:
输入:n = 1 输出:0
提示:
0 <= n <= 5 * 106
如果
设
我们在
时间复杂度
class Solution:
def countPrimes(self, n: int) -> int:
primes = [True] * n
ans = 0
for i in range(2, n):
if primes[i]:
ans += 1
for j in range(i + i, n, i):
primes[j] = False
return ans
class Solution {
public int countPrimes(int n) {
boolean[] primes = new boolean[n];
Arrays.fill(primes, true);
int ans = 0;
for (int i = 2; i < n; ++i) {
if (primes[i]) {
++ans;
for (int j = i + i; j < n; j += i) {
primes[j] = false;
}
}
}
return ans;
}
}
class Solution {
public:
int countPrimes(int n) {
vector<bool> primes(n, true);
int ans = 0;
for (int i = 2; i < n; ++i) {
if (primes[i]) {
++ans;
for (int j = i; j < n; j += i) primes[j] = false;
}
}
return ans;
}
};
func countPrimes(n int) int {
primes := make([]bool, n)
for i := range primes {
primes[i] = true
}
ans := 0
for i := 2; i < n; i++ {
if primes[i] {
ans++
for j := i + i; j < n; j += i {
primes[j] = false
}
}
}
return ans
}
/**
* @param {number} n
* @return {number}
*/
var countPrimes = function (n) {
let primes = new Array(n).fill(true);
let ans = 0;
for (let i = 2; i < n; ++i) {
if (primes[i]) {
++ans;
for (let j = i + i; j < n; j += i) {
primes[j] = false;
}
}
}
return ans;
};
public class Solution {
public int CountPrimes(int n) {
var notPrimes = new bool[n];
int ans = 0;
for (int i = 2; i < n; ++i)
{
if (!notPrimes[i])
{
++ans;
for (int j = i + i; j < n; j += i)
{
notPrimes[j] = true;
}
}
}
return ans;
}
}