描述:

给个整数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. (转)RESTful API 设计最佳实践

    原文:http://www.oschina.net/translate/best-practices-for-a-pragmatic-restful-api 数据模型已经稳定,接下来你可能需要为web ...

  2. react中父组件调用子组件的方法

    1.直接使用ref进行获取 import React, {Component} from 'react'; export default class Parent extends Component ...

  3. pidgin-lwqq

    今天第一次用pidgin登上了qq,好神奇. 从网上看到的,说是pidgin发布了支持webqq协议的lwqq,按照解释之后安装了. 感觉好神奇啊. 一个绿色的小企鹅^^ 今天有看到了一个大牛,我觉得 ...

  4. 神之编辑器emacs

    vim被称之为编辑器之神,而emacs被成为神之编辑器. 可以当编辑器,也可以当做编译器. 编辑好后保存 输入 M-x shell 可以编译文件 g++ test.cpp -o test ./test ...

  5. anrdroid AVD启动不起来的问题。Waiting for HOME ('android.process.acore') to be launched

    Waiting for HOME ('android.process.acore') to be launched 卡在这里了. 可以到sdk mananager里面先启动起来AVD,然后在eclip ...

  6. HDFS 和 YARN 的 HA 故障切换【转】

    来源:https://blog.csdn.net/u011414200/article/details/50336735 一 非 HDFS HA 集群转换成 HA 集群二 HDFS 的 HA 自动切换 ...

  7. 【转】提高你开发效率的十五个Visual Studio 2010使用技巧

    原文网址:http://developer.51cto.com/art/201203/321942.htm 2012-03-06 14:38 Ron Ngai Ron Ngai的博客 字号:T | T ...

  8. php switch case的"bug"

    首先说明,这不是一个bug.应该说是一个比较容易中招的陷阱. 今天使用switch遇到一个问题,代码如下: <?php $num = 0; switch ($price) { case $pri ...

  9. Windows Server 2012/win8 iis8 上安装 asp.net 4.5 当时用了mvc5 .net framework 4.5 所以得装下

    vs2013+mvc5 +.net framework 4.5  本地 iisexpress 调试一点问题没有,当部署到本机iis时 出现 无法识别 modules错误,具体错误提示是: 锁定是默认设 ...

  10. Java之dom4j的简单解析和生成xml的应用

    一.dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,它的性能超过sun公 ...