LeetCode 204. Count Primes计数质数 (C++)
题目:
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.
分析:
统计所有小于非负整数 n 的质数的数量。
这里使用埃拉托斯特尼筛法。要得到自然数n以内的全部素数,必须把不大于√n的所有素数的倍数剔除,剩下的就是素数。
例如我们要求2-25以内素数的个数,
2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25
第一次先剔除掉2以后的倍数
2,3,5,7,9,11,13,15,17,19,21,23,25
接下来剔除3的倍数
2,3,5,7,11,13,17,19,23,25
接下来剔除5的倍数
2,3,5,7,11,13,17,19,23
由于7 > √25,算法停止。
我们可以初始化大小为n的数组res,其内所有元素均为1,默认所有数字均为素数,首先将0,1剔除掉,然后开始执行算法,当前元素i,若res[i]==1,将i的倍数全部置为0,若res[i]==0,则继续执行,注意本题是求小于n的素数的个数,终止条件设为小于sqrt(n)即可,若包括n,则需要小于等于sqrt(n)。
程序:
class Solution {
public:
int countPrimes(int n) {
if(n < ) return ;
vector<int> res(n, );
res[] = ;
res[] = ;
for(int i = ; i < sqrt(n); ++i){
if(res[i] != )
continue;
for(int j = i*; j < n; j += i){
res[j] = ;
}
}
int num = count(res.begin(), res.end(), );
return num;
}
};
LeetCode 204. Count Primes计数质数 (C++)的更多相关文章
- [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 (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- 204 Count Primes 计数质数
计算所有小于非负整数 n 的质数数量. 详见:https://leetcode.com/problems/count-primes/description/ Java实现: 埃拉托斯特尼筛法:从2开始 ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- [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
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- Java for LeetCode 204 Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...
- Leetcode 204 Count Primes 数论
题意:统计小于n的质数个数. 作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法. 是的,素数筛法并不是该题最佳的解法,线性素数筛法才是. 至于什么是素数筛法,请百度吧. class Sol ...
随机推荐
- 安装Rtools
1.好多工具需要安装Rtools install.packages("installr") install.packages("stringr") ###依赖包 ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things 水题
A. Forgetting Things Kolya is very absent-minded. Today his math teacher asked him to solve a simple ...
- ARC082E ConvexScore(神奇思路)
这题就是拼拼凑凑就出来了. 可能看英文题面容易题意杀(小写大写 \(n,N\)),这里复述一遍:对于每个构成凸多边形的点集(每个点恰好都是凸多边形的顶点,必须是严格的凸多边形,内角严格小于 180 度 ...
- Jupyter Notebook使用
不论你是刚开始学 Python,还是正在啃数据分析的骨头,对你来说,不断在各种命令行窗口和编辑器里切来切去,或者不断打开各种窗口查看 matplotlib 的输出之类的繁琐操作,一定是家常便饭了.哎呀 ...
- (四十二)golang--协程之间通信的方式
假设我们现在有这么一个需求: 计算1-200之间各个数的阶乘,并将每个结果保存在mao中,最终显示出来,要求使用goroutime. 分析: (1)使用goroutime完成,效率高,但是会出现并发/ ...
- ideal 切换git和svn
原文地址:https://blog.csdn.net/lixld/article/details/98851427 intellij ideal gi和svn切换: 之前项目是svn的,新的项目用了g ...
- lego loam 跑镭神32线激光雷达
师弟反应镭神32线激光雷达(32C)录制的数据包不能跑lego loam,这里就总结一下. 首先lego loam默认的接受的topic name是velodyne_points,点云的frame_i ...
- Asp.NET Core Nginx Ocelot ForwardedHeaders X-Forwarded-For
ocelot在部署时我使用了nginx作为转发,并配置了https证书,但是发现ocelot不支持Forward host header. https://ocelot.readthedocs.io/ ...
- colmap编译过程中出现,无法解析的外部符号错误 “__cdecl google::base::CheckOpMessageBuilder::ForVar1(void)”
错误提示: >colmap.lib(matching.obj) : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: cl ...
- Git上传到码云及其常见问题详解
1.git init 初始化 2.git remote origin add https://gitee.com/su_yong_qing/SyqSystem.git 这里注意把链接替换为自己的仓库 ...