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. DMP文件的生成和使用

    1.生成dmp的程序 #include  <dbghelp.h> #pragma comment(lib,  "dbghelp.lib") //设置异常处理回调函数Se ...

  2. Delphi XE中类成员的访问权限(新增了strict private和strict protected,还有automated)

    Delphi XE中类成员的访问权限共提供了6个关键词来用于限定访问权限:public.private.protected.published.automated strict private . s ...

  3. delphi中用代码实现注册Ocx和Dll(有点怪异,使用CallWindowProc来调用指定函数DllRegisterServer)

    在windows系统中,可以通过Regsvr32来实现注册ocx或者dl, 编程时,调用Regsvr32来注册,却不能正常执行.尤其是在Win7系统中,需要管理员身份才能运行. 使用下面的代码则能正常 ...

  4. 人脸对齐ASM-AAM-CLM的一些总结

    源地址:http://blog.csdn.net/piaomiaoju/article/details/8918107 ASM算法相对容易,其中STASM是目前正面脸当中比较好的算法,原作者和CLM比 ...

  5. 高度关注!国务院对A股发出强烈信号↓

    高度关注!国务院对A股发出强烈信号↓http://dwz.cn/2qHBd1郎咸平:中国股市存在一大隐疾 使其成为全球市场的一个另类!http://dwz.cn/2qHBVy一不小心,马云又完成了四场 ...

  6. LeetCode——Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. JavaEE session机制

    JavaEE session机制 Http协议: 在讲session之前,必须说下Http协议,HTTP是一个client和server端请求和应答的标准(TCP).由HTTPclient发起一个请求 ...

  8. Linux中下载,压缩,解压等命令

    查看是否和还有一台Linux机器相通命令:ssh    主机名@Ip地址    ,提示输入password.就可以查看远程文件的文件夹 下载远程机器上的文件:scp  主机名@Ip地址:/path/s ...

  9. C语言 - 结构体(struct)比特字段(:) 详细解释

    结构体(struct)比特字段(:) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26722511 结构体(struc ...

  10. 调用微信退款接口时,证书验证出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法

    1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应用程序池-->右击-->高级设置- ...