注:质数从2开始,2、3……

改进过程:

一、常规思路是对小于n的每一个数进行isPrime判断,isPrime(int x)函数中for(int i = 2; i <= x /2; ++i),如果x%i==0,return false。

二、将isPrime(int x)中i的范围改为i * i <= x。不用i <= sqrt(x),因为sqrt耗时。

三、isPrime中把是2、3、5倍数的数直接return,不进入for循环。

四、对于每一个数p,p2、p2+p、p2+2p……均不为素数,有点像动态规划的思想。此时可以不用isPrime(int x)函数,而是维护一个isPrime[n]的bool数组,先设里面所有的值均为true,然后i从3开始循环(对应的是如果输入n<3,直接return 0)。因为会把i2之后的一系列数在isPrime数组中的值变为false,因此i循环至i * i < n。

关于if(!isPrime[i]) continue;这一句解释一下。如果isPrime[i] == false,那么也就是说在之前的i循环里被标记过了。设之前的循环的数为p,那么 i = p+ np,如果没有continue这一句的话,那么 j = i * i,设j也等于p2 + mp,于是得到等式 (p+ np)2 = p+ mp,解得m = p((p + n)2 - 1),也就是说j之前在p循环时已经被标记过了,因此可以直接continue,节省时间。以下是代码,也是题目提示中给出的最优代码。

 1 public int countPrimes(int n) {
 2    boolean[] isPrime = new boolean[n];
 3    for (int i = 2; i < n; i++) {
 4       isPrime[i] = true;
 5    }
 6    // Loop's ending condition is i * i < n instead of i < sqrt(n)
 7    // to avoid repeatedly calling an expensive function sqrt().
 8    for (int i = 2; i * i < n; i++) {
 9       if (!isPrime[i]) continue;
10       for (int j = i * i; j < n; j += i) {
11          isPrime[j] = false;
12       }
13    }
14    int count = 0;
15    for (int i = 2; i < n; i++) {
16       if (isPrime[i]) count++;
17    }
18    return count;
19 }

五、在最后计算cnt时,从2开始到n-1都被判断了一遍,但是偶数绝对不是素数,因此是没必要访问其在数组isPrime中的值的。

既然计算cnt时可以跳过偶数,那么在之前的标记isPrime的for循环里,也可以跳过偶数,相应的j每次+= 2 * i,因为p(p + 1)必定为偶数。

所以for循环里依次变为i += 2, j += 2 * i, i += 2(上一步为++i, j += i, ++i)。

最终版代码

 1 class Solution {
 2 public:
 3     int countPrimes(int n) {
 4         if(n < 3) return 0;
 5         bool* isPrime = new bool [n];
 6         for(int i = 3; i < n; ++i)
 7             isPrime[i] = true;
 8
 9         for(int i = 3; i * i <= n; i += 2){
10             if(!isPrime[i]) continue;
11             for(int j = i * i; j < n; j += 2 * i)
12                 isPrime[j] = false;
13         }
14
15         int cnt = 1;
16         cout<<isPrime[9]<<endl;
17         for(int i = 3; i < n; i += 2){
18             if(isPrime[i]) ++cnt;
19         }
20         return cnt;
21     }
22 };

转载于:https://www.cnblogs.com/co0oder/p/5286781.html

LeetCode 204. Count Primes相关推荐

  1. leetCode 204. Count Primes 哈希 求素数

    204. Count Primes 求素数 Description: Count the number of prime numbers less than a non-negative number ...

  2. [LeetCode] 204. Count Primes

    204. Count Primes Count the number of prime numbers less than a non-negative number, n. Example: Inp ...

  3. LeetCode -- 204. Count Primes

    题目标签 HashTab(哈希表) 题意及思路 题意:略 思路:有关素数的题目我所知道有两种做法.一种是最基本的isPrime算法,关键点在循环判断时,上限为Math.sqrt(n) (求n是否为素数 ...

  4. 【LeetCode 剑指offer刷题】特殊数题3:204 Count Primes

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 204. Count Primes Count the number of prime numbers less t ...

  5. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  6. 204. Count Primes

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  7. leetcode python3 简单题204. Count Primes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零四题 (1)题目 英文: Count the number of prime ...

  8. letecode [204] - Count Primes

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  9. Leetcode-204 Count Primes

    #204 Count Primes Count the number of prime numbers less than a non-negative number, n. 题解:这道题如果对每个小 ...

最新文章

  1. php各种编码集详解和在什么情况下进行使用 [php 字符集 显示]
  2. 是否有一个不区分大小写的string.Replace的替代方法?
  3. 服务器端利器--双缓冲队列
  4. 思想已经高过行动好多了
  5. MongoDB副本集同步原理
  6. centos装java配件_CentOS安装JAVA
  7. C语言排序方法-----二元选择排序法
  8. easyUI不同版本的combotree控件clear方法的区别
  9. C#基础12.1:Object类
  10. php增加html元素,使用php将appendChild($ element)添加到现有的html元素中
  11. python 指定时间运行代码
  12. Python | 实现双色球选号(educoder)
  13. protoc执行命令
  14. 下城投 × 奇点云 |「数智城投驾驶舱」,打造转型示范新样板
  15. android 2.1你好八月,八月你好优美句子
  16. LCD和OLED显示屏有什么区别?
  17. 伽罗华域(Galois Field)理解、基于伽罗华域的四则运算(附详细python代码)
  18. 电赛知识补充——电机篇
  19. Windows各个版本系统的官网地址
  20. 22个Python迷你程序,最适合你拿来学习练手了

热门文章

  1. 关于 not enough actual parameters for macro ...
  2. linux tune2fs命令详解
  3. VIM使用系统剪切板
  4. [react] React的isMounted有什么作用?
  5. [react] React为什么要搞一个Hooks?
  6. React开发(139):react中onref
  7. 前端学习(3122):react-hello-react总结state
  8. 前端学习(2933):vue中的循环语句
  9. 前端学习(2618):vue插槽--默认插槽
  10. 前端学习(2532):vuex mutation