[LeetCode] 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] Count Primes 质数的个数的更多相关文章
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)
题意:给一个数n,返回小于n的素数个数. 思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的, ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
- Python3解leetcode Count Primes
问题描述: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Outpu ...
- Count Primes ----质数判断
质数的判断 埃拉托斯特尼筛法: 算法的过程如下图所示: 我们从2开始遍历到根号n,先找到第一个质数2,然后将其所有的倍数全部标记出来,然后到下一个质数3,标记其所有倍数,依次类推,直到根号n,此时数组 ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- LeetCode OJ:Count Primes(质数计数)
Count the number of prime numbers less than a non-negative number, n. 计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如 ...
- Count Primes - LeetCode
examination questions Description: Count the number of prime numbers less than a non-negative number ...
随机推荐
- C#的Process类调用第三方插件实现PDF文件转SWF文件
在项目开发过程中,有时会需要用到调用第三方程序实现本系统的某一些功能,例如本文中需要使用到的swftools插件,那么如何在程序中使用这个插件,并且该插件是如何将PDF文件转化为SWF文件的呢?接下来 ...
- C# WinForm制作电子琴键盘
上一篇 http://hovertree.com/h/bjaf/y8qol2p4.htm 再上一篇的基础上,使用WinForm制作了一个电子琴键盘: 演示地址 http://hovertree.com ...
- 使用VS Code从零开始开发调试.NET Core 1.0
使用VS Code 从零开始开发调试.NET Core 1.0. .NET Core 是一个开源的.跨平台的 .NET 实现. VS Code 全称是 Visual Studio Code,Visua ...
- 商业智能软件对比评测:FineBI和Tableau
FineBI和Tableau是比较好的自助式商业智能软件,功能都很强大,是企业数据可视化不可或缺的利器,但两款产品还是有非常大的区别的,例如Tableau的功能全面且深入,更适合专业的数据分析人员,而 ...
- JavaScript学习(零)前引
一)概述 JavaScript是一个面向web的编程语言,一种解释性语言,边执行边解释.也是一种基于对象(Object)和事件驱动(EventDriven)的,安全性好的脚本语言,语法和java类似. ...
- 离开Autodesk,开启新篇章
我已经离开了Autodesk,开启新篇章.在过去7年多时间中,我先后支持Autodesk 基础设施相关产品的开发,包括MapGuide/AIMS,Map3D,Civil 3D,Infraworks等, ...
- iOS提交AppStore后申请加急审核
提交审核后进去下面链接申请加急审核链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在I would l ...
- fastjson 混淆注意事项
使用fastjson 注意事项,主要表现: 1.加了符号Annotation 的实体类,一使用就会奔溃 2.当有泛型属性时,一使用就奔溃 在调试的时候不会报错,当你要打包签名混淆包的时候,就会出现上述 ...
- http转https 和 微信小程序设置了合法请求域名,小程序一直提示不在合法域名列别中
hotapp 有免费的https proxy ,可以免费代理请求任何http或者https服务,只要设置好合法域名为https://wxapi.hotapp.cn , 就可以请求网址如请求小程序联盟的 ...
- 【代码笔记】iOS-柱状图
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...