当前位置:网站首页>计数质数[枚举 -> 空间换时间]
计数质数[枚举 -> 空间换时间]
2022-06-27 21:24:00 【REN_林森】
前言
对于枚举法/暴力法,都是有着较高的时间复杂度。原因可能是没有用到题目已给的显示条件/需要挖掘的隐式个性,也有可能是很多重复计算,需要空间记录从而缓解时间消耗。
一、计数质数

二、枚举->空间换时间
1、暴力枚举
// 计数质数
public class CountPrimes {
/* target:给定一个非负整数n,返回小于n的所有质数。 什么是质数?除了1和本身,没有其他因数 的大于1的自然数。 */
// 暴力解
public int countPrimes(int n) {
// 特殊情况,特殊处理。
if (n < 2) return 0;
// 判定每个数是否为素数。
int ans = 1;// 先把2算。
for (int i = 3; i < n; i++) if (isTrue(i)) ++ans;
return ans;
}
private boolean isTrue(int n) {
// 剪枝,只需要验证到sqrt(n)即可,两边镜像。如 2 * 4 == 8 == 4 * 2
int bound = (int) Math.sqrt(n);
for (int i = 2; i <= bound; i++) if (n % i == 0) return false;
return true;
}
}
2、空间换时间
class CountPrimes2 {
/* target:给定一个非负整数n,返回小于n的所有质数。 什么是质数?除了1和本身,没有其他因数 的大于1的自然数。 暴力解超时,如果能把非质数挑出来,那么剩下的就算质数了,非质数都是 质数与质数/非质数相乘。 为什么没有非质数 与这些相乘?因为非质数可被拆解成一大一小的数,而小的数已经把后面所有倍数 的非质数标记了。如 8 == 2 * 4,8 * n == 2 * (4 * n) 可将质数的每个倍数都标记为 非质数,然后往后走,没被标记的则是质数,再把该质数的所有倍数(除前面已经标记的,所以从i * i开始)标记为非质数。 */
// 空间换时间,记录非素数,寻找素数。
public int countPrimes(int n) {
// 特殊情况,特殊处理。
if (n < 2) return 0;
// 标记非质数,寻找质数个数。
boolean[] isNotPrim = new boolean[n];
int ans = 0;
for (int i = 2; i < n; i++) {
if (!isNotPrim[i]) {
// 未被标记成非质数,则其是质数。
++ans;
// 标记非质数。
// 从i * i开始,毕竟轮到i-1时,它乘过i,i+1,...
if (i < Math.sqrt(n)) for (int j = i * i; j < n; j += i) isNotPrim[j] = true;
}
}
return ans;
}
}
总结
1)从暴力枚举,到空间换时间(利用题目隐式特征)。
参考文献
[1] LeetCode 计数质数
边栏推荐
- const关键字及其作用(用法),C语言const详解
- clickonce 部署ClickOnce应用程序时出错-清单中的引用与下载的程序集的标识不匹配
- What if Fiddler fails to listen to the interface
- 【PCL自学:Segmentation3】基于PCL的点云分割:区域增长分割
- 往前一步是优秀,退后一步是懵懂
- Instructions for vivado FFT IP
- Windows环境下的ELK——Logstash+Mysql(4)
- vivado VIO IP的用法
- [AI application] detailed parameters of NVIDIA Tesla v100-pcie-32gb
- ClickOnce error deploying ClickOnce application - the reference in the manifest does not match the identity of the downloaded assembly
猜你喜欢

An analysis of C language functions

Structure de stockage des graphiques

c语言字符指针、字符串初始化问题

图的存储结构
![[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer](/img/38/c7ce908bfcc5cc5cd5856996aa015b.png)
[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer

UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation!

2022年PMP项目管理考试敏捷知识点(3)

Cornernet understands from simple to profound

Introduction to quantitative trading

2022 PMP project management examination agile knowledge points (3)
随机推荐
Does the subscription of Siyuan notes stop deleting cloud data directly?
[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation
halcon之区域:多种区域(Region)特征(6)
【剑指Offer】47. 礼物的最大价值
matlab axis坐标轴相关设置详解
Pat class B 1013
100 questions for an enterprise architect interview
c语言之字符串数组
Golang uses Mongo driver operation - query (basic)
Swing UI——容器(一)
Zero foundation self-study SQL course | if function
获取基因有效长度的N种方法
Webserver flow chart -- understand the calling relationship between webserver modules
圖的存儲結構
Cornernet由浅入深理解
Elk in Windows Environment - logstash+mysql (4)
[sword finger offer] 48 Longest substring without duplicate characters
手把手教你移植 tinyriscv 到FPGA上
webserver流程图——搞懂webserver各模块间调用关系
golang使用mongo-driver操作——查(进阶)