LeetCode204:Count Primes
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的更多相关文章
- LeetCode----204. Count Primes(Java)
package countPrimes204; /* * Description: * Count the number of prime numbers less than a non-negati ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
- 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 ...
- HDU 5901 Count primes 论文题
Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- hdu 5901 Count primes (meisell-Lehmer)
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- LeetCode_204. Count Primes
204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- 204. Count Primes - LeetCode
Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...
随机推荐
- asp.net(C#)之NPOI"操作Excel
1.首先到网上下载"NPOI.DLL".引用. 2.新建一个操作类"ExcelHelper.cs": using System.Collections.Gene ...
- [IDEs]Eclipse自动格式化代码
格式化代码快捷键:Ctrl + Shift + F 一般情况: 1).Ctrl + A 2).Ctrl + Shift + F ps: 格式化之后发现代码换行了,因为已经达到最大长度,可修改设置,增加 ...
- iOS中的字符串NSString
创建一个字符串对象: NSstring * str1 = @"hello world"; NSString * str = [[NSString alloc]initWithStr ...
- 智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用
智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用 先上图: 现在的智能控制都是基于微控制器,随着智能的手持终端的普及,基于智能终端的控制就会越来越普遍. WIFI便是其中的一 ...
- 正确理解HTML,XHTML页面的头部doctype定义
摘自http://www.west263.com/info/html/wangyezhizuo/css/20080225/42390.html 当我们制作页面的时候,总会在它的源代码头部看到一串声明, ...
- MTK Android Driver:GPIO
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2JrODYxMTEw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- (hdu 简单题 128道)平方和与立方和(求一个区间的立方和和平方和)
题目: 平方和与立方和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(一)——初识WiX
原文:使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(一)--初识WiX Visual Studio 打包安装七宗罪 开发.NET的人,肯定会使用Visual ...
- hdu3811(状态压缩dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3811 题目大意:给定1~N个数,求出至少满足一个条件的排列总数.M个条件如下:Ai位置的数为Bi 分析 ...
- hdu 4404 Worms(多边形与圆的交)
求出爆炸点的坐标,就成了多边形与圆相交面积的模板题了... #include<algorithm> #include<iostream> #include<cstring ...