原题

原题链接

Description:

Count the number of prime numbers less than a non-negative number, n.

计算小于非负数n的素数个数。

思路

这题用埃拉托斯特尼筛法来做效果比较好,普通的方法基本会TLE。但是在用了埃拉托斯特尼筛法之后,还有一些细节值得注意:

(1)首先我们该用 i*i<=n 替代 i<=sqrt(n) 来避免使用 sqrt() ,因为sqrt()的操作是比较expensive的。

(2)当要表示两个状态的时候,首选是用bool而不是int来节省空间。这个以前也是知道的,但是具体写代码的时候经常没注意。

(3)埃拉托斯特尼筛法只需要计算到 i*i<n 的部分即可。

代码

class Solution {
public:
int countPrimes(int n) {
if(n<=2) return 0;
bool res[n];
int ans=1;
int i,j;
for(i=3;i<n;i++)
if(i%2==0) res[i]=false;
else res[i]=true;
for(i=3;i*i<n;i+=2){
if(!res[i]) continue;
for(j=i;j*i<n;j+=2)
res[i*j]=false;
}
for(int i=3;i<n;i+=2)
if(res[i]) ans++;
return ans;
}
};

LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes的更多相关文章

  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 质数的个数

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

  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计数质数 (C++)

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

  5. 统计所有小于非负整数 n 的质数的数量,埃拉托斯特尼筛法

    素数的定义:质数又称素数.一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数. 1.暴力算法: 令i=2; 当i<n的时候,我们循环找出2-i的质数,即让i%(2~i-1), ...

  6. 洛谷P3383 【模板】线性筛素数 (埃拉托斯特尼筛法)

    题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行 ...

  7. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  8. LeetCode 204 Count Primes

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

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

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

随机推荐

  1. 6、第六课,js jquery20150928

    1.声明事件,给事件添加动作 function MyClick() { alert("这是我的第一个js") } 2.js特点 A.js区分大小写 B.弱类型变量,定义一个变量 v ...

  2. Java HttpClient

    public class WebClient { public static final String POST_TYPE_JSON = "json"; public static ...

  3. 引用类型和原始类型的对比(java)

    Java 提供两种不同的类型:引用类型和原始类型(或内置类型).另外,Java 还为每个原始类型提供了封装类(Wrapper). 原始类型 封装类=================boolean Bo ...

  4. 再谈Cookies欺骗

    在上一篇关于cookies欺骗的随笔中,提到的解决方案是把密码MD5加密之后存入cookies中,确实这种方法实现了效果,不过把密码留在客户端等待着去被破解不是一个合适的方法,在此也感谢 @老牛吃肉 ...

  5. 我对Backbone中url属性的理解

    Model中有一个url属性,而且有一个urlRoot属性. Collection中也有一个url属性. // 这是Model中的url方法 url: function() { var base = ...

  6. 用css3实现闪烁效果

    1. css3 @keyframes 参考 css3 @keyframes规则. 特别注意浏览器支持: Internet Explorer 10, Firefox, 和 Opera 支持 @keyfr ...

  7. C++中的函数指针

    寒假这些天在看<The C++ Programming Language, 3rd>. 今天看到Chapter7 Function,里头好一些东西是C语言里没有的,比如overload.p ...

  8. validate()的配置项

    1.submitHandler //通过验证成功后运行的函数 代码: $("#mainForm").validate({ ...... rules:{ username:{//此处 ...

  9. swf上传

    swfupload多文件异步上传 多文件选择异步上传的原理 传统上:多个文件逐一选.PHP开始处理,循环上 PHP+Flash上:JS调用flash控,Flash批量选取并保持选取所有文件列 swfu ...

  10. 不在界面上用控件 动态创建idhttp,IdAntiFreeze来用

    不在界面上用控件 动态创建idhttp,IdAntiFreeze来用 var IdHTTP: Tidhttp; IdAntiFreeze: TidAntiFreeze; begin IdAntiFre ...