【刷题-LeetCode】204. Count Primes
- 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.
解法1 埃氏筛法
class Solution {
public:
int countPrimes(int n) {
if(n < 2)return 0;
vector<bool>isPrime(n, true);
isPrime[0] = false;
isPrime[1] = false;
int ans = 0;
for(int i = 2; i < n; ++i){
if(isPrime[i]){
ans++;
int k = 2;
while(k * i < n){
isPrime[k*i] = false;
k++;
}
}
}
return ans;
}
};
解法2 欧拉筛法(线性筛)
class Solution {
public:
int countPrimes(int n) {
if(n < 3)return 0;
vector<bool>isPrime(n, true);
vector<int>Prime(n);
isPrime[0] = false;
isPrime[1] = false;
int cnt = 0;
for(int i = 2; i <= n -1 ; ++i){
if(isPrime[i]){
Prime[cnt++] = i;
}
for(int j = 0; j < cnt && i * Prime[j] < n; ++j){
isPrime[Prime[j]*i] = false;
}
}
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 ...
- 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 质数的个数
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
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 ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- leetcode 204. Count Primes(线性筛素数)
Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题. c ...
- 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 ...
随机推荐
- AT2664 [AGC017A] Biscuits 题解
Content 有一个长度为 \(n\) 的数列 \(a\).你希望从中选出一些数,使得这些数的和对 \(2\) 取模后的结果为 \(P\).求方案数. 数据范围:\(1\leqslant n\leq ...
- CF70B Text Messaging 题解
Content 有一个短信软件最多只能够上传长度为 \(n\) 的消息.现在你有一段话,但不一定能够一次发出.这段话由若干句话组成,以 ..? 或者 ! 为结尾.你不能够将一句话拆开来发,但是如果容量 ...
- LuoguP7694 [COCI2009-2010#4] AUTORI 题解
Content 科学论文会大量引用一些早期的著作,因此在一个论文中出现两种不同的命名约定并不少见.这两种不同的命名约定分别是: 长变体,由每个作者姓氏的完整单词由连字符连接而成,例如 Knuth-Mo ...
- InnoDB学习(五)之数据库锁
InnoDB存储引擎的默认隔离级别事可重复读,MVCC多版本并发控制仅仅解决了快照读情况下的数据隔离,而对于当前读,InnoDB通过锁来进行并发控制. InnoDB锁 本文主要参考了MySQL官方文档 ...
- .NET 云原生架构师训练营(对象过程建模)--学习笔记
目录 UML OPM OPM优化 UML 1997年发布UML标准 主要域 视图 图 主要概念 结构 静态视图 类图 类.关联.泛化.依赖关系.实现.接口 用例视图 用例图 用例.参与者.关联.扩展. ...
- Java整合redis报错s if RDB snapshotting fails (stop-writes-on-bgsave-error option)
Caused by: io.lettuce.core.RedisCommandExecutionException: MISCONF Redis is configured to save RDB s ...
- 如何下载哔哩哔哩、爱奇艺、腾讯视频、优酷、斗鱼、TED、YouTube网页视频
这里使用you-get工具进行下载 github地址:https://github.com/soimort/you-get/ github项目文档:https://github.com/soimort ...
- Linux c++编译总结(持续更新)
1. 没有定义的符号 这类的错误, 解决办法:A. 添加对应的头文件(源文件), B.前置声明 1.1 错误描述: error: variable has incomplete type 'class ...
- 【LeetCode】672. Bulb Switcher II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- D. Ability To Convert
http://codeforces.com/contest/758/problem/D D. Ability To Convert time limit per test 1 second memor ...