欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

204. Count Primes 统计<n的质数的个数

发布时间:2023/12/24 48 如意码农
生活随笔 收集整理的这篇文章主要介绍了 204. Count Primes 统计<n的质数的个数 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

[抄题]:

Count the number of prime numbers less than a non-negative number, n.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

质数的倍数是合数

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

质数的倍数是合数

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int countPrimes(int n) {
//cc
if (n < 2) {
return 0;
} //ini
int count = 0;//prime num
int[] notPrime = new int[n];//not prime num //for i, i * j
for (int i = 2; i < n; i++) {
if (notPrime[i] == 0) count++;
for (int j = 2; i * j < n; j++) {
notPrime[i * j] = 1;
}
} return count;
}
}

总结

以上是生活随笔为你收集整理的204. Count Primes 统计&lt;n的质数的个数的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。