描述:

给个整数n,计算小于n的素数个数。

思路:

埃拉托斯特尼筛法,其实就是普通筛子,当检测到2是素数,去除所有2的倍数;当检测到3是素数,去除其倍数。

不过这要求空间复杂度为n,时间复杂度为n。

解决:

int countPrimes(int n) {
if (n < )
return ;
int count = ;
bool* no = new bool[n]{n, false}; for(int i = ; i < n; i+=) {
if (!no[i]) {
count++;
long j = (long)i * i;
for (; j < n; j += i*) // 此处优化重要!
no[j] = true;
}
}
return count;
}

有几个注意的地方。

1. no数组用new,不要用vector,由于操作简单。

2. 更新时,从i * i开始,比i小的数,都给比i小的质数给更新了。

3. 每次步进2i,如果一次步进i,则为2i,偶数。

leetcode 204 count prim 数素数的更多相关文章

  1. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  2. leetcode 204. Count Primes(线性筛素数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题. c ...

  3. [LeetCode] 204. Count Primes 计数质数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  4. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

  5. [LeetCode] 204. Count Primes 质数的个数

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

  6. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  7. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  8. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

  9. LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes

    原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...

随机推荐

  1. 从HDU2588:GCD 到 HDU5514:Frogs (欧拉公式)

    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...

  2. mongodb 修改openfiles方法

    # vi /etc/security/limits.conf 在file中加上以下内容:(注意红色的为添加的) #ftp hard nproc 0#@student - maxlogins 4* so ...

  3. 设计模式(Python)-单例模式

    本系列文章是希望将软件项目中最常见的设计模式用通俗易懂的语言来讲解清楚,并通过Python来实现,每个设计模式都是围绕如下三个问题: 为什么?即为什么要使用这个设计模式,在使用这个模式之前存在什么样的 ...

  4. 为已编译的DLL附带强命名

    在我们开发的过程中,会经常调用其他人写好的DLL类库,由于种种的原因,不管是公司规定,还是个人习惯等等的原因,有时候需要调用各个类库直接邀请必须强命名. 但是我们临时也无法找到源代码进行重新编译等事情 ...

  5. mysql master master slave 环境搭建

    master1:192.168.128.47 master2:192.168.128.96 slave:192.168.128.97   master1与master2互备,master2作为slav ...

  6. oracle12c之 表空间维护总结

    1.1.创建永久表空间 In the CDB:SQL> CONNECT system@cdb1SQL> CREATE TABLESPACE cdb_users DATAFILE'/home ...

  7. 函数前修饰const与函数名后修饰const

    #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #inc ...

  8. 【python】函数名存在变量中

    变量函数:意思就是将函数名存在变量中,然后根据变量值动态的调用需要的函数. LOGIN = 'xxxx' PASSWD = "xxx" URL = 'xxxxx' def hand ...

  9. 智能家居入门DIY——【五、执行命令】

    前面几篇介绍了ESP8266使用AT命令来连接WIFI实现一系列功能.这一篇介绍一下使用Wemos D1 Wifi来进行开发,当然也可以用常见的8针ESP8266来完成(只是需要按网上的方法将Ardu ...

  10. python mysql模块

    多次使用python操作mysql数据库,先与大家分享一下,关于如何使用python操作mysql数据库.mysql并不是python自带的模块,因此需要下载安装.(在windows平台下介绍该使用过 ...