[LeetCode] 204. Count Primes 解题思路
Count the number of prime numbers less than a non-negative number, n.
问题:找出所有小于 n 的素数。
题目很简洁,但是算法实现的优化层次有很多层。其中最主要思想的是采用 Sieve of Eratosthenes 算法来解答。
大思路为:
- 找出 n 范围内所有合数,并做标记。
- 未做标记的即为素数,统计未做标记的数个数即为原题目解。
如何找到 n 范围内所有合数?
将第一个素数 2 赋值给 i。
当 i 小于 n 时:(2)
- 对于以确定的素数 i ,将 i 的全部倍数标记为合数。(1)
- 离 i 最近的下一个未被标记为合数的数即为素数。将下一个素数赋值给 i .
上面算法有可以优化的地方:
(1)步骤找合数,无需从 2 开始算 i 的倍数,而是从 i 倍开始算,即 i*i。举个例子,当 i 为 5 时, 5*2, 5*3, 5*4 的记号,已经在 i 分别为 2,3,4的时候做了。所以,可以直接从 i 倍开始算。相应地,(2)步骤也可以优化 “为 i*i < n 时”。
int countPrimes(int n) {
vector<bool> res(n, true);
for(long i = ; i * i < n ; i++){
if(res[i] == false){
continue;
}
long square = i*i;
for(long k = ; k * i + square < n ; k++){
res[k * i + square] = false;
}
}
int cnt = ;
for(int i = ; i < n ; i++){
cnt += res[i];
}
return cnt;
}
[LeetCode] 204. Count Primes 解题思路的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
- [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 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes
原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
随机推荐
- datazen logo修改
第一步:进入cp 页面 第二步: P161说明文档的P161开始,这里有说有版本的图片命名 第三步:再次强调第二步的命名,否则 如果你直接复制这个名字,不但效果出不来,还删除不掉这个包,我上次是把da ...
- C#中创建线程,创建带参数的线程
线程操作主要用到Thread类,他是定义在System.Threading.dll下.使用时需要添加这一个引用.该类提供给我们四个重载的构造函 构造函数定义: 无参数委托 [SecuritySafeC ...
- Web字体库下载及转换工具
1.字体现在网站: http://ztxz.org/ 2.Web字体在线格式转换器: http://www.freefontconverter.com/ 3.
- L-value 和 R-value.
An L-value is something that can appear on the left side of an equal sign, An R-value is something t ...
- MD5算法的使用
using System.Security.Cryptography; using System.Text; StringBuilder sb = new StringBuilder();//构建一个 ...
- Linux shell日常命令和技巧
转自:http://www.vaikan.com/linux-shell-tips-and-tricks/ 原文:http://www.techbar.me/linux-shell-tips/ 使用L ...
- 【USACO 3.2.5】魔板
[描述] 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色.这8种颜色用前8个 ...
- Redis学习 - 入门
业精于勤,荒于嬉:行成于思,毁于随 -- 韩愈·<进学解> 因为工作中需要用到Redis,所以最近抽点时间看了一下,现在将学习的内容整理一下. 一.简介 1.Redis是什么? R ...
- 两个示例介绍JavaScript的闭包
JavaScript的闭包有两个用途:一个是访问函数内部的变量:另一个是让变量的值在作用域内保持不变.函数是JavaScript 中唯一有作用域的对象,因此JavaScript的闭包依赖于函数实现,下 ...
- bootstrap瀑布流代码
<extend name="Base/common" /> <block name="search-cate"> <include ...