原题
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