Description:

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

比计算少n中素数的个数。

素数又称质数,是指仅仅能被1和它自身相除的自然数。

须要注意的是1既不是素数也不是合数。

2是最小的素数。

使用推断一个数是否是素数的函数,那么这个函数须要进行一轮循环,在给定的小于n中又要进行一轮循环。所以时间复杂度是O(n^2)。

能够对推断一个数是否是素数的函数进行优化。对于数i,能够仅仅对2到√i之间的数进行推断。这样时间复杂度减少到了O(nlogn)。

可是上面的解法在leetcode中还是超时。

于是想是否存在仅仅进行一轮循环的方法。即在遍历1至n-1一次的过程中记录下素数的个数。可是后面就不知道怎么处理。

然后看leetcode中的小提示,发现了一种更优的寻找素数的方法。首先看以下的这个图:

这个图事实上就道出了这个算法是怎么进行的。使用一个长度是n的hash表,最開始这个hash表中的全部元素都是没有被处理的,从2開始遍历,假设这个元素没有被处理,那么将素数的个数加1,然后将2*2,2*3,2*4……2* k( 2* k < n)标记为已经被处理了的。接着開始处理3,同理将3*2,3*3,3*4…..3*m( 3 * m < n)标记为已被处理了的,接着是4,因为这个元素已经被处理。继续向后遍历。这样一直处理下去。

从这道题中又意识到了一个整数会溢出会导致问题的小技巧。

两种解法分别例如以下:

class Solution {
public:
/*
//解法一:超时
int countPrimes(int n) {
int count=0;
for(int i=2;i<=n;i++)
{
if(isPrime(i))
count++;
}
return count;
} bool isPrime(int n)
{
if(n==1)
return false;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
return false;
}
return true;
}
*/
//解法二:
int countPrimes(int n) {
int * mask=new int[n]();//能够在这里直接对动态数组进行初始化
int count=0;
for(int i=2;i<n;i++)
{
if(mask[i]==0)
{
count++;
for(int j=2;i*j<n;j++)//这里不能将j初始化成i,否则i*j会溢出
{
mask[i*j]=1;
}
} }
return count; }
};

版权声明:本文博主原创文章,博客,未经同意不得转载。

LeetCode204:Count Primes的更多相关文章

  1. LeetCode----204. Count Primes(Java)

    package countPrimes204; /* * Description: * Count the number of prime numbers less than a non-negati ...

  2. [leetcode] Count Primes

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

  3. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  4. HDU 5901 Count primes 论文题

    Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...

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

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

  6. hdu 5901 Count primes (meisell-Lehmer)

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. LeetCode_204. Count Primes

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

  8. 【刷题-LeetCode】204. Count Primes

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

  9. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

随机推荐

  1. 改动Oracle GoldenGate(ogg)各个进程的读检查点和写检查点

    请注意:请谨慎改动Oracle GoldenGate(ogg)各个进程的读检查点和写检查点. 请确保已经 掌握 ogg 各个进程的读检查点和写检查点的详细含义. BEGIN {NOW | yyyy-m ...

  2. Oracle中四种循环(GOTO、For、While、Loop)

    DECLARE x number; BEGIN x:=9; <<repeat_loop>> --循环点 x:=x-1; DBMS_OUTPUT.PUT_LINE(X); IF ...

  3. nginx+tomcat负载均衡策略

    測试环境均为本地,測试软件为: nginx-1.6.0,apache-tomcat-7.0.42-1.apache-tomcat-7.0.42-2.apache-tomcat-7.0.42-3 利用n ...

  4. Delphi过程函数传递参数的八种方式

    今天一同事问我为什么有些过程函数里面有Var而有些没有,不解,遂到网上百度,得解.快哉,快哉. 在Delphi过程.函数中传递参数几个修饰符为Const.Var.Out.另一种不加修饰符的为默认按值传 ...

  5. 解压tar.gz文件报错gzip: stdin: not in gzip format解决方法

    解压tar.gz文件报错gzip: stdin: not in gzip format解决方法 在解压tar.gz文件的时候报错 1 2 3 4 5 [Sun@localhost Downloads] ...

  6. A Game of Thrones(18) - Catelyn

    “We will make King’s Landing within the hour.” Catelyn turned away from the rail and forced herself ...

  7. fzu2150(bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 题意:在任意两处点火,求最短时间烧光所有草堆. 分析:由于n,m比较小,将所有草堆坐标记录下来,然后暴力 ...

  8. 聊聊高并发(三十八)解析java.util.concurrent各个组件(十四) 理解Executor接口的设计

    JUC包中除了一系列的同步类之外,就是Executor运行框架相关的类.对于一个运行框架来说,能够分为两部分 1. 任务的提交 2. 任务的运行. 这是一个生产者消费者模式,提交任务的操作是生产者,运 ...

  9. 从SAE又回到BAE,感觉好轻松

    [前言] 我这个人总喜欢对同一类东西比較过来比較过去,用过来用过去. 比如曾经选择浏览器,从開始ie,到遨游,世界之窗.qq等等,用了有10款左右的浏览器,每款都用了不短时间, 终于固定在火狐+chr ...

  10. IntelliJ IDEA中怎样使用JUnit4

     背景 近期參与了一个Anroid医疗项目,当中项目底层有非常多基础类及通讯类,并且非常多涉及复杂的字节操作还有多线程同步及状态机处理.这种项目做一下TDD还是必要的,尽量项眼下期把风险减少一些. ...