原题
Description:
Count the number of prime numbers less than a non-negative number, n.
代码实现
public int CountPrimes(int n) {
int rtncnt = 0;
bool[] notPrimes = new bool[n];
for (int i = 2; i < n; i++)
{
if (notPrimes[i]) continue;
rtncnt++;
for (int j = 2; i*j < n; j++)
{
notPrimes[i * j] = true;
}
}
return rtncnt;
}
leetcode-solution库
leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp

本文提供了一种计算小于非负整数n的素数数量的方法。通过筛选法标记合数,遍历数组来统计素数个数。此算法在LeetCode上实现并开源于GitHub。
689

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



