Count the number of prime numbers less than a non-negative number, n.

Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

这道题给定一个非负数n,让我们求小于n的质数的个数,题目中给了充足的提示,解题方法就在第二个提示 埃拉托斯特尼筛法 Sieve of Eratosthenes 中,这个算法的过程如下图所示:

我们从2开始遍历到根号n,先找到第一个质数2,然后将其所有的倍数全部标记出来,然后到下一个质数3,标记其所有倍数,一次类推,直到根号n,此时数组中未被标记的数字就是质数。我们需要一个 n-1 长度的 bool 型数组来记录每个数字是否被标记,长度为 n-1 的原因是题目说是小于n的质数个数,并不包括n。 然后来实现埃拉托斯特尼筛法,难度并不是很大,代码如下所示:

class Solution {
public:
int countPrimes(int n) {
int res = ;
vector<bool> prime(n, true);
for (int i = ; i < n; ++i) {
if (!prime[i]) continue;
++res;
for (int j = ; i * j < n; ++j) {
prime[i * j] = false;
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/204

类似题目:

Ugly Number II

Ugly Number

Perfect Squares

参考资料:

https://leetcode.com/problems/count-primes/

https://leetcode.com/problems/count-primes/discuss/57588/My-simple-Java-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 204. 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] Count Primes 质数的个数

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

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

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

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

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

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

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

  6. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  7. Leetcode 204 Count Primes 数论

    题意:统计小于n的质数个数. 作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法. 是的,素数筛法并不是该题最佳的解法,线性素数筛法才是. 至于什么是素数筛法,请百度吧. class Sol ...

  8. Java [Leetcode 204]Count Primes

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

  9. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

随机推荐

  1. 安装docker后修改docker文件目录

    docker会下载容器,运行会挂载磁盘,所以我们需要把docker装在大容量的分区. 安装 https://docs.docker.com/install/linux/docker-ce/centos ...

  2. JavaScript的__proto__、prototype和继承

    JavaScript也是可以“继承”的! 各位看官或是好奇,或是一知半解.什么是prototype,__proto__,constructor.哪种继承方式好.今天就在这交流交流. 什么是protot ...

  3. 【mybatis】mybatis传参的几种方式

    参考地址: https://my.oschina.net/liuzelin/blog/2966633

  4. xiaohacontainer, docker, windows-来自微软Azure CTO的布道

    https://azure.microsoft.com/zh-cn/blog/containers-docker-windows-and-trends/ 今天这个时代当你讨论云计算时,不谈谈docke ...

  5. 十三道Python练习题

    一.完美立方 编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组(a, b, c, d),使得a^3= b^3 + c^3 + d^3,其中a,b,c,d 大于 1, 小于等于N. 输入 ...

  6. vue.js 父组件主动获取子组件的数据和方法、子组件主动获取父组件的数据和方法

    父组件主动获取子组件的数据和方法 1.调用子组件的时候 定义一个ref <headerchild ref="headerChild"></headerchild& ...

  7. 基于rtmp+nginx 、vlc实现FFmpeg推流与wpf端拉流

    这周在研究基于rtmp+nginx直播流的实现,现总结如下: 0.所需文件: 链接:https://pan.baidu.com/s/1U5gsNI8Rcl684l5gVL6swg 提取码:dli9 1 ...

  8. JavaScript函数式编程究竟是什么?

    摘要: 理解函数式编程. 作者:前端小智 原文:JS中函数式编程基本原理简介 Fundebug经授权转载,版权归原作者所有. 在长时间学习和使用面向对象编程之后,咱们退一步来考虑系统复杂性. 在做了一 ...

  9. Python从零开始——迭代器与生成器

    一:迭代器 二:生成器

  10. 其他综合-搭建本地yum仓库及自制rpm包

    搭建本地yum仓库及自制rpm包 实验目的 为方便本地 yum 的管理,建本地 yum 仓库,实现局域网内部快速安装常用软件 实验环境 VMware:12版本 系统版本:CentOS Linux re ...