leetcode 204 count prim 数素数
描述:
给个整数n,计算小于n的素数个数。
思路:
埃拉托斯特尼筛法,其实就是普通筛子,当检测到2是素数,去除所有2的倍数;当检测到3是素数,去除其倍数。
不过这要求空间复杂度为n,时间复杂度为n。
解决:
int countPrimes(int n) {
if (n < )
return ;
int count = ;
bool* no = new bool[n]{n, false}; for(int i = ; i < n; i+=) {
if (!no[i]) {
count++;
long j = (long)i * i;
for (; j < n; j += i*) // 此处优化重要!
no[j] = true;
}
}
return count;
}
有几个注意的地方。
1. no数组用new,不要用vector,由于操作简单。
2. 更新时,从i * i开始,比i小的数,都给比i小的质数给更新了。
3. 每次步进2i,如果一次步进i,则为2i,偶数。
leetcode 204 count prim 数素数的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- leetcode 204. Count Primes(线性筛素数)
Description: Count the number of prime numbers less than a non-negative number, n. 题解:就是线性筛素数的模板题. 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 解题思路
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
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. 解题思路: 空间换时间,开一个空间 ...
- (easy)LeetCode 204.Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...
- 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的 ...
随机推荐
- erhai系统使用_web
使用前说明1.安装mysql数据库,安装数据库管理器EMS(SQL Manager Lite for MySQL),将数据库导入数据库管理器: 注意对配置文件my.ini的修改.2.启动resin W ...
- AI人工智能专业词汇集
作为最早关注人工智能技术的媒体,机器之心在编译国外技术博客.论文.专家观点等内容上已经积累了超过两年多的经验.期间,从无到有,机器之心的编译团队一直在积累专业词汇.虽然有很多的文章因为专业性我们没能尽 ...
- HDU 4635
http://acm.hdu.edu.cn/showproblem.php?pid=4635 问:最多加多少条边,使得原图不是强连通图 正向考虑有困难,不妨反向思考,既最少去掉几条边使得原图不是强连通 ...
- Java第四次作业--面向对象高级特性(继承和多态)
一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握类的继承概念和设计 掌握构造方法的继承原则 掌握方法重写 掌握super键字和final关键字 理解多态的概念,掌握通过方法重写和方法重载机制 ...
- PyQt4 ShowHMDB show sqlite3 with QTableWidget summary
PyQt4 ShowHMDB show sqlite3 with QTableWidget summary Source Code: https://github.com/zengjfgit/Pyth ...
- IDEA 使用generator逆向工程生成pojo,mapper
1.新建立一个MAVEN项目 2.在pom.xml增加配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns: ...
- Python3.x datetime模块
1.时间间隔(timedelta) 指定时间长度之间计算差值 #!/usr/bin/env python __author__ = 'realtiger' """ @ve ...
- nats 学习 request/reply 模式基本使用
nats 一个云原生的消息系统,使用简单,客户端丰富,支持的模式是pub/sub 但是集成比较灵活,可以支持loadblance, request/reply pub/sub 代码演示的是reques ...
- FastAdmin 开发时如何与官方同步升级
FastAdmin 开发时如何与官方同步升级 使用 FastAdmin 开发时为了与官方同步升级,推荐使用 git 管理代码. 官网上提供的完整包是为了方便第一次使用的人快速测试. 我一般是给官方的 ...
- log4net 使用指南,最常遇到的问题整理。。。
一. Log4net特征 Log4net是一个用于.NET开发环境的日志记录组件,由于它的超快及超灵活,很多大型的应用都会用到. 它有如下特点: 1.自定义日志输出级别 ...