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 ...
随机推荐
- [PHP] laravel5.5 搭建流程
1.nginx 配置 server { listen 80; server_name laravel5-test.d.com; charset utf-8; location ...
- [SpingBoot guides系列翻译]文件上传
文件上传 这节的任务是做一个文件上传服务. 概况 参考链接 原文 thymeleaf spring-mvc-flash-attributes @ControllerAdvice 你构建的内容 分两部分 ...
- Java连载8-基本数据类型2
一.基本数据类型 1.字符串“abc”不属于基本数据类型,属于引用数据类型 2. 基本数据类型 占用空间大小(单位:字节) byte 1 short ...
- STS 重写父类方法的操作
本来这种东西真的没什么好写的,但是很多时候真的是要用到的,不知道的话自己手动敲,会累死人的.所以记录在这里,自己的笔记,有需要的赶紧拿去,省的手动录入那么辛苦. 在代码窗口点击右键,依次选择“Sour ...
- F#周报2019年第18期
新闻 FableConf 2019开始征集提案 2019理事会选举 如同DSL一般的Elmish封装器fable-elmish,可以创建控制台或者终端界面 介绍VS Code的远程开发 F#(.NET ...
- DevExpress的图形按钮菜单栏控件WindowsUIButtonPanel的布局、使用和设置按钮的点击事件
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- vue 脚手架搭建步骤!
========================================================== 说出来都是泪,最开始都不知道从哪里开始(回头一看还是很简单的,关键是要找到入口) ...
- python 对过时类或方法添加删除线的方法
class Cat(Animal): def __init__(self): import warnings warnings.warn("Cat类带删除线了", Deprecat ...
- ASCII,Unicode,UTF-8,GBK 区别
编码历史与区别 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为"字节". 再后来,他们又 ...
- CSS自定义右键菜单
<style> .menu { width: 100px; font-size: 14px; font-family: "微软雅黑"; border: 1px soli ...