leetcode:Count Primes
Description:Count the number of prime numbers less than a non-negative number, n.
本题给定一个非负数n,让我们求小于n的质数的个数,解题方法就在第二个提示埃拉托斯特尼筛法Sieve of Eratosthenes中,这个算法的过程如下图所示,我们从2开始遍历到根号n,先找到第一个质数2,然后将其所有的倍数全部标记出来,然后到下一个质数3,标记其所有倍数,一次类推,直到根号n,此时数组中未被标记的数字就是质数。我们需要一个n-1长度的bool型数组来记录每个数字是否被标记,长度为n-1的原因是题目说是小于n的质数个数,并不包括n。 然后我们用两个for循环来实现埃拉托斯特尼筛法,难度并不是很大,代码如下所示:
class Solution {
public:
int countPrimes(int n) {
vector<bool> num(n - 1, true);
num[0] = false;
int res = 0, limit = sqrt(n);
for (int i = 2; i <= limit; ++i) {
if (num[i - 1]) {
for (int j = i * i; j < n; j += i) {
num[j - 1] = false;
}
}
}
for (int j = 0; j < n - 1; ++j) {
if (num[j]) ++res;
}
return res;
}
};
其他解法:
1、(56ms)
class Solution {
public:
int countPrimes(int n) {
if (n < 2)
{
return 0;
}
bool prime[n];
memset(prime, true, n*sizeof(bool)); //memset:作用是在一段内存块中填充某个给定的值,第三个参数指定块的大小
prime[0] = false;
prime[1] = false; int result = 0;
int limit = sqrt(n); for (int i = 2; i <= limit; i++)
{
if (prime[i])
{
for (int j = i*i; j < n; j += i)
{
prime[j] = false;
}
}
} for (int i = 0; i < n; i++)
{
if (prime[i])
{
result++;
}
} return result;
}
};
2、(86ms)
class Solution {
public:
int countPrimes(int n) {
if(n<3)
return 0;
int *flag=new int[n];
fill(flag,flag+n,1);//fill()作用是设置指定范围【flag,flag+n)内的元素值为1
int c=n-2,m=n/2;//1和n都不在,故为n-2
for(int i=2;i<=m;i++)
{
if(flag[i])
{
for(int j=2;i*j<n;j++)
{
if(flag[i*j])
{
flag[i*j]=0;
c--;
}
}
}
}
delete []flag;
return c;
}
};
leetcode:Count Primes的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- LeetCode 204. Count Primes计数质数 (C++)
题目: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: ...
- LeetCode OJ:Count Primes(质数计数)
Count the number of prime numbers less than a non-negative number, n. 计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如 ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- Java for LeetCode 204 Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...
- 【leetcode】Count Primes(easy)
Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...
随机推荐
- load d3dcompiler_46.dll failed
https://gist.github.com/rygorous/7936047 编shader的时候遇到这个warning不知道是不是什么隐患..从今天开始要做新项目了 尝试从同事那里要了这dll ...
- VS开发工具 不会在异常的地方停止的问题.
启用"仅我的代码"
- ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正
为UINavigationBar 写一个分类.UINavigationItem+correct_offset.h 转载 http://www.colabug.com/thread-1112420-1 ...
- 批量安装操作系统之cobbler
Cobbler 部署文档 服务端配置 操作系统:Centos6.4 关闭防火墙及 selinux 安装cobbler软件 添加yum源 rpm -Uvh https://dl.fedoraprojec ...
- ExtJS与jQuery的一点细节上的对比
首先说明这不是一篇完整解读ExtJS和jQuery所有方面差异的文章,只是针对我个人刚看了两天的jQuery产生的一些疑问的整理.之前用过一段时间ExtJS,了解ExtJS的一些机制.现在做移动开发, ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- vs2010创建并使用DLL
一.为什么需要dll 代码复用是提高软件开发 效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架, 如ATL ...
- ExtJs之Ext.query
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- [你必须知道的.NET]第二十九回:.NET十年(上)
发布日期:2009.05.08 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. /// <summary> /// 本文部分内容,已 ...
- poj 3228(二分+最大流)
题目链接:http://poj.org/problem?id=3228 思路:增设一个超级源点和一个超级汇点,源点与每一个gold相连,容量为gold数量,汇点与仓库相连,容量为仓库的容量,然后就是二 ...