[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的质数 ...
随机推荐
- jquery ajax 提交表单(file && input)
用到的插件 jquery.js jquery.form.js[http://malsup.github.io/jquery.form.js] 提交页面 <form enctype="m ...
- Android客户端与服务端交互之登陆示例
Android客户端与服务端交互之登陆示例 今天了解了一下android客户端与服务端是怎样交互的,发现其实跟web有点类似吧,然后网上找了大神的登陆示例,是基于IntentService的 1.后台 ...
- 【转】Java 读写Properties配置文件
[转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...
- <c:if>判断参数是否同时为空
<c:if test="${empty str}"> str为空</c:if> <c:if test="${not empty str}& ...
- gephi安装好了,为何打不开?
ref: http://www.zhihu.com/question/21268129?sort=created 这个软件我自己也没有弄过,不过我同学要不会装,所以我测试地帮她装,得益于这个哥们的发的 ...
- 关于模板中的动态取值 ---反射与javascript脚本编译
在项目中经常遇到一个问题,打印word或者打印excel的时候,我们经常使用一对一的赋值或者批量替换的方式来对模板进行修改. 但是现在遇到两种场景: 1.取值是通过自定以方法进行取值的. 如:一个销售 ...
- C#制作ActiveX控件及部署升级(摘自网络)
使用C#开发ActiveX控件 控件开发.制作CAB包.签名.部署 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序 ...
- extjs之messagebox按钮显示中文
在extjs中我们用到了很多的提示框,但是在对话框按钮上面显示的都是英文的ok.yes.no.cancel,这里给出一个方法,能够使得提示框的按钮文本显示问中文. extjs在加载完成之后会调用页面的 ...
- grails框架中在使用domain的save方法保存时保存不成功
1.如果报错,自行根据异常查找错误,这里不说明 2.如果为报错,我遇到的就是domain中的字段属性与数据库中为同步 (1)你的domain是新的,在增加新的字段属性时未使用update更新数据库,造 ...
- 正则如何匹配div下的所有<li>标签?
<?php header('Content-Type:text/html;charset=utf-8'); $str = '<div class="c1s"> & ...