Description:
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
public int countPrimes(int n) {
int count = 0;
for (int i = 2; i < n; i++)
if (isPrime(i))
count++;
return count;
}

博客介绍了如何优化LeetCode第204题的解决方案,通过改进isPrime()算法,减少计算量,将判断条件从小于sqrt(n)的所有数改为小于sqrt(n)的素数,从而避免超时并提高计算效率。
订阅专栏 解锁全文
128

被折叠的 条评论
为什么被折叠?



