204. Count Primes

来源:互联网 发布:ipad防沉迷软件 编辑:程序博客网 时间:2024/06/03 18:00

原题

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

2 0
原创粉丝点击