[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 的素数. 题目很简洁,但是算法实现的 ...
随机推荐
- Vue全选和全不选
HTML代码: <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> ...
- linux基础学习路线&review
linux基础学习网址: https://www.runoob.com/linux/linux-tutorial.html 比较重点的是这个启动过程的介绍学习:https://www.runoob.c ...
- 【Zabbix】zabora批量部署
zabora简化批量部署 目的:简化部署zabora,批量监控数据库的常用指标 1 数据库用户赋权 上传cre_arp_monitor.sh,并且部署用户. [root@oradb ~]# chown ...
- LINQ 之 LookUp
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 本文作者文采欠佳,文字表达等方面不是很好,但实际的代码例子是非常实用的,请作参考. 一.先准备要使用的类: 1.Person类: cl ...
- Spring Security OAuth2 Demo —— 客户端模式(ClientCredentials)
前情回顾 前几节分享了OAuth2的流程与其它三种授权模式,这几种授权模式复杂程度由大至小:授权码模式 > 隐式授权模式 > 密码模式 > 客户端模式 本文要讲的是最后一种也是最简单 ...
- Asp.Net或WebAPI获取表单数据流(批量文件上传)
//Web或WebAPI获取表单数据流(批量文件上传) public JsonResult UploadFile() { //HttpPostedFi ...
- WPF控件模板(6)
什么是ControlTemplate? ControlTemplate(控件模板)不仅是用于来定义控件的外观.样式, 还可通过控件模板的触发器(ControlTemplate.Triggers)修改控 ...
- postman强大的团队协作功能
今天无意在调项目的接口 ,使用的postman工具 ,自己写的接口信息,只能自己看 ,感觉有点不方便,如果一个公司多名测试,如果一个人写接口信息,大家都能用,就会很节约时间 所以团队协作的功能就诞生了 ...
- Lucene 写入一个文档到该文档可搜索延迟是多少?
我看的是最初版的lucene,1.4.3 结论是新写入的文档会先写入内存中,只有当到达一定阈值后才会刷新进磁盘,而搜索可以搜索到的数据由最初定义IndexSearcher时磁盘里的段数据决定,如果想要 ...
- 操作系统原理之I/O设备管理(第六章上半部分)
一.I/O系统的组成 I/O系统不仅包括各种I/O设备,还包括与设备相连的设备控制器,有些系统还配备了专⻔⽤ 于输⼊/输出控制的专⽤计算机,即通道.此外,I/O系统要通过总线与CPU.内存相连. I/ ...