leetcode 204
题目描述:
Description:
Count the number of prime numbers less than a non-negative number, n.
解法一:
遍历从1-n的所有整数,查看是否为质数,是质数借助一个则将该整数存入一个容器中,判断一个数是否为质数,可以遍历在容器中且小于n的平方根的质数,如果n可以被符合条件的质数整除,则这个数不是质数。代码如下:
class Solution {
public:
vector<int> prime_vec;
bool isPrime(int n)
{
if (n<)
return false;
else if (n == )
return true;
else if (n % == )
return false;
else
{
int n_sqr = sqrt(n);
for (int i = ; prime_vec[i] <= n_sqr; i ++) {
if(n % prime_vec[i] == )
return false;
}
return true;
}
}
int countPrimes(int n) {
int counter = ;
for (int i = ; i < n; i ++) {
if (isPrime(i)) {
prime_vec.push_back(i);
counter ++;
}
}
for (auto i = prime_vec.begin(); i != prime_vec.end(); i ++) {
cout << *i << endl;
}
return counter;
}
};
解法二:
上述代码的执行效率不高,看了其他人的解题思路之后,豁然开朗,维基百科上有一个动态演示的效果图,算法思想名叫“晒数法”,连接如下:
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
看了这个效果图我马上进行了自己的实现,代码如下:
class Solution {
public:
int countPrimes(int n) {
int counter = ;
if (n < )
return counter;
int upper = sqrt(n);
vector<bool> flag_vec(n, false);
int i = ;
while (i < n) {
cout << i << endl;
counter ++;
if(i <= upper)
{
for (long long j = i * i; j < n; j += i)
flag_vec[j] = true;
}
++ i;
while (flag_vec[i] == true)
++ i;
}
return counter;
}
};
可以很清楚地知道,解法二比解法一要好很多,因为在从小到大遍历的过程中,所有的数仅遍历一遍,这解法一虽然比暴力的O(n*n)的方法好一些,但是时间复杂度还是大于O(n)的,而晒数法的时间复杂度仅为O(n)。
leetcode 204的更多相关文章
- [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 (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- Leetcode 204计数质数
计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 比计算少n中素数的个数. 素数又称质 ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Java实现 LeetCode 204 计数质数
204. 计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . class Solutio ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- 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 ...
随机推荐
- Codeforces558E A Simple Task(线段树)
题目 Source http://codeforces.com/problemset/problem/558/E Description This task is very simple. Given ...
- React的双向绑定
以前对于双向绑定概念来自于Angular.js,现在我用我感兴趣的react.js来实现这样的方式.有2种方式分析,1:不用插件,2:用插件 (引入react.js操作省略...) 不用插件: 先创建 ...
- maven archetype:generate 命令简化项目模板数量
在maven里使用 mvn archetype:generate 来创建项目是十分方便的,但有时也不尽然.在网络不好时,从网络上加载 archetype-catalog.xml文件(http://re ...
- 【手把手教你Maven】构建过程
Maven是一款进行 依赖管理.项目构建.信息管理 为一体的工具. 它不像Make具有复杂的命令.也不像Ant需要手动编写大量的重复代码就能进行项目的构建: 还能提供强大的依赖库管理,避免jar包混乱 ...
- 百度网盘生成二维码api
分享出自精神,灵感来自大脑,在百度云网盘分享每一个文件,都会在页面生成一个二维码扫描的图片: 我就进一步看了该图片的地址: 发现没有,圈圈内是不是有点眼熟,就跟其他二维码api接口一样,只要盗用这段东 ...
- maxscale
一.maxscale简介1.MaxScale是maridb开发的一个mysql数据中间件,其配置简单,能够实现读写分离,并且可以根据主从状态实现写库的自动切换.2.官网:https://mariadb ...
- 掌握Thinkphp3.2.0----标签库
1.什么是内置标签?什么是标签扩展库? Cx.class.php 和 Html.class.php 2.怎么加载非内置标签,怎么使用? 两种方式加载 3.怎么扩展自定义的标签? 仿照Html.clas ...
- 使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程
下载 Xcode 8,配置 iOS 10 和 Swift 3 (可选)通过命令行编译 除 非你想使用命令行编译,使用 Swift 3.0 的工具链并不需要对项目做任何改变.如果你想的话,打开 Xcod ...
- pyspider 安装时 Could not run curl-config
sudo aptitude install libcurl4-openssl-dev
- MySQL 主从配置
mysql主从复制指两个服务器之间数据库的同步,当主服务器的数据进行了变更,从服务器也会自动更新,其过程是通过bin-log日志实现的,本质是binlog日志的传输. mysql主从分两个角色 1.主 ...