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. Android中网络流量控制(防火墙)——Iptables

    Iptables简单介绍 iptables是与最新的 2.6.x 版本号 Linux 内核集成的 IP 信息包过滤系统. 假设 Linux 系统连接到因特网或 LAN.server或连接 LAN 和因 ...

  2. Xamarin 安装教程 支持Visual Studio 2013

    本文的前提是你已经正确的安装了VS 2013. 本文的全部步骤在Win7 Ultimate 64系统上測试通过.支持VS 2013,我用的版本号是VS 2013 update2. 安装 1.      ...

  3. Maven聚合

    <project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2 ...

  4. [置顶] iframe使用总结(实战)

    说在前面的话,iframe是可以做很多事情的. 例如: a>通过iframe实现跨域; b>使用iframe解决IE6下select遮挡不住的问题 c>通过iframe解决Ajax的 ...

  5. (ZT)LoadRunner9.0成功破解方法

    LoadRunner9.0软件下载地址: http://www.3atesting.com/filedown/LR9Download.exe 破解所需文件 http://download.csdn.n ...

  6. hdu-1016素数环

    这个题就是给出一个数字n,表示有n个数.编号为1~n. 然后要求我们将这n个数连起来变成一个环,要求随意两个数相加所得值必须为素数. 假设满足条件就将这个环输出来! 这个题:dfs+回溯+推断.然后注 ...

  7. ASA虚墙配置

    asa配置ASA Version 8.0(2) <system>!hostname ASA5520enable password 2KFQnbNIdI.2KYOU encryptedno ...

  8. ReentrantLock可重入锁的使用场景(转)

    摘要 从使用场景的角度出发来介绍对ReentrantLock的使用,相对来说容易理解一些. 场景1:如果发现该操作已经在执行中则不再执行(有状态执行) a.用在定时任务时,如果任务执行时间可能超过下次 ...

  9. 金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on Xanga

    金句: "對比MBA學位,我們更需要PSD學位的人!" Poor, Smart and Deep Desire to… | consilient_lollapalooza on X ...

  10. hdu 1561 The more, The Better (依赖背包 树形dp)

    题目: 链接:点击打开链接 题意: 非常明显的依赖背包. 思路: dp[i][j]表示以i为根结点时攻击j个城堡得到的最大值.(以i为根的子树选择j个点所能达到的最优值) dp[root][j] = ...