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的更多相关文章

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

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

  2. LeetCode 204. Count Primes计数质数 (C++)

    题目: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: ...

  3. LeetCode OJ:Count Primes(质数计数)

    Count the number of prime numbers less than a non-negative number, n. 计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如 ...

  4. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  5. [LeetCode] 204. Count Primes 计数质数

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

  6. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  7. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  8. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  9. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

随机推荐

  1. [noi2011]道路修建 树形dp

    这道题可以说是树形dp的入门题,也可以看成是一道检验[树]这个数据结构的题目: 这道题只能bfs,毕竟10^6的复杂度win下肯定爆栈了: 但是最恶心的还不是这个,实测用printf输出 用cout输 ...

  2. JavaScript之Throw、Try 、Catch讲解

    try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. 错误一定会发生 当 JavaScript 引擎执行 JavaScript 代码时,会发生各种错误: 可能是 ...

  3. 持久化消息队列memcacheq的安装配置

    MemcacheQ 是一个基于 MemcacheDB 的消息队列服务器. 一.memcacheq介绍 特性: 1.简单易用 2.处理速度快 3.多条队列 4.并发性能好 5.与memcache的协议兼 ...

  4. group_concat

    分割字符串 group_concat(a.EvidencesID separator ',') as EvidencesID #在MySQL配置文件(my.ini)中默认无该配置项,使用默认值时,值为 ...

  5. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  6. js小技巧(二)

    //移动的图层,拖动 1.<span style='position:absolute;width:200;height:200;background:red' onmousedown=Mous ...

  7. Dom新find

    1.HTML标签和属性是不区分大小写的,但JS是区分大小写的:所以(1)HTML专有的接口的属性应该以小写字母开头,如果属性名由多个单词构成,第二个及接下来的每个单词的首字母都要大写.(2)有些HTM ...

  8. CSS3做小三角形

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXgAAAA2CAIAAABC2hVZAAAgAElEQVR4nKzcd3cbV57web+1p20FW8

  9. HTML基本操作

    插入图片: 1.利用链接(静态) <img src="http://www.kmwzjs.com/useruploads/images/20101020_057600100825157 ...

  10. Linux系统新手学习的11点建议

    随着Linux应用的扩展许多朋友开始接触Linux,根据学习Windwos的经验往往有一些茫然的感觉:不知从何处开始学起.这里介绍学习Linux的一些建议. 一.从基础开始:常常有些朋友在Linux论 ...