[LeetCode] 204. Count Primes 质数的个数
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
类似题目:
参考资料:
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 质数的个数的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- LeetCode 204. Count Primes计数质数 (C++)
题目: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- Leetcode 204 Count Primes 数论
题意:统计小于n的质数个数. 作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法. 是的,素数筛法并不是该题最佳的解法,线性素数筛法才是. 至于什么是素数筛法,请百度吧. class Sol ...
- 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 解题思路
Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...
随机推荐
- 【shell脚本】自动监控tomcat服务===autoCheck.sh
自动监控tomcat服务,当tommcat服务挂掉时自动重启 一.脚本内容 [root@localhost ]# cat /root/autoCheck.sh #!/bin/bash startTom ...
- vuex 源码分析(五) action 详解
action类似于mutation,不同的是Action提交的是mutation,而不是直接变更状态,而且action里可以包含任意异步操作,每个mutation的参数1是一个对象,可以包含如下六个属 ...
- oracle排序子句的特殊写法与ORA-01785错误
刚刚写的SQL语句在执行的时候报[ORA-01785: ORDER BY item must be the number of a SELECT-list expression]错误,于是自己百度了一 ...
- 转 tty 设备读写
转自https://feng-qi.github.io/2017/05/04/how-to-read-write-to-tty-device/ <p>这是 StackExchange 上的 ...
- 动态ALV表实例-移动类型汇总
TABLES:MSEG,MAKT. "定义结构 TYPES:BEGIN OF TY_DATA, MJAHR LIKE MSEG-MJAHR, "物料凭证的年份 MBLNR LIKE ...
- 第九届极客大挑战——小帅的广告(二阶sql注入)
也是经过一通扫描和测试,没发现其他有用信息,感觉这是个sql注入.其实对于二阶sql注入我以前没实践过,也没看过资料,只是知道这个名字,但不知道为何看到这道题就让我回想起了这个名词,所以查了一下二阶s ...
- JDK1.8 —— 接口定义增强
使用default和static定义接口方法 JDK1.8(jre8)以后,接口中不在仅仅只允许定义抽象方法,开始允许定义普通方法了:而普通方法需要用default声明. interface IMes ...
- Modbus协议 CRC 校验码
CRC(循环冗余校验)在线计算 http://www.ip33.com/crc.html 里面的8005的多项式值,但网上看到的算法都是用A001来异或的 ---------------------- ...
- tornado 之 RequestHandler(请求)
RequestHandler from tornado.web import ReuqestHandler 一.利用HTTP协议想服务器传递参数 提取url的特定部分 http://127.0.0.1 ...
- Linux目录管理
Linux文件目录管理 1:目录管理 1)切换目录 # cd [ 目录名称] 2)退到上一目录 # cd .. 2:创建目录 mkdir [文件名称] mkdir -p [文件名称] 递归创建目 ...