原题

原题链接

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. jdbc03 使用servlet实现

    <%@page import="cn.bdqn.bean.News"%> <%@page import="cn.bdqn.service.impl.Ne ...

  2. HDU3757

    题意:一些团队因为任务要去避难所,并且每个避难所必须要有团队在,避难所的数量小于等于团队的数量, 团队去避难所的消耗油量与路程成正比,求解最小耗油量.题目来源:2010 Northeastern Eu ...

  3. oracle的一知半解

    这里只讲第一次开发运用oracle数据库的.net程序遇到问题: 1.程序与oracle数据库在同一台的服务器,貌似设置好连接字符串就可以直接访问( 需要主要的问题: 字符串格式:Data Sourc ...

  4. form表单提交

    1.form表单提交.html页面失败 <%--客户端form--%> <form id="form2" action="LoginOne.html&q ...

  5. MySQL导入导出命令

    前言 如果使用图形化界面,那么可以通过几个点击即可导入.导出.本文是假定你没有安装那些如Navicat等GUI管理软件. 场景 假设在电脑A和电脑B中都装有MySQL数据库管理系统,并且在电脑A的My ...

  6. An attempt to attach an auto-named database for file

    在用VS自带的 .mdf读取(joint)时,报错: Server Error in '/' Application. An attempt to attach an auto-named datab ...

  7. Linux 所有命令都用不了,只有cd exit能用

    原因: 在设置 java环境变量时,编辑profile文件没有写正确,导致在命令行下 ls等命令不能够识别.在命令行下打入下面这段就可以了export PATH=/usr/local/sbin:/us ...

  8. Qt5如何设置静态编译,解决生成的可执行文件打开出错问题

    将https://yunpan.cn/cqGGURjmG2fEY  访问密码 8de5  中的压缩包Qt5-MSVC-Static-master.zip 解压到你的qt安装目录,一般就是C:\Qt下, ...

  9. delphi2010 开发及调试WebService 实例

    使用delphi已经10多年了,一直搞桌面程序开发,对Webservice一直很陌生,近来因工作需要,学习delphi开发WebService,担心遗忘,作此笔记. 特别感谢 中塑在线技术总监 大犇 ...

  10. CLR via C#字符串和文本处理

    一.字符   在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发.   每个字符都表示成System.Char结构(一个值类型) 的一个实例.Sy ...