厄拉多塞筛选法,就是哈希表记录素数的倍数

public int countPrimes(int n) {
/*
牛逼哄哄的厄拉多塞筛选法
就是从2开始,每找到一个素数,就把n以内的这个数的倍数排除
记录倍数的可以用一个Boolean数组
这个题放在Hashtable里的原因可能就是因为这个方法把
*/
//这个题没说明白,其实不包括n
boolean[] not = new boolean[n];
int res = 0;
for (int i = 2; i < n; i++) {
if (!not[i])
res++;
for (int j = 2; j*i < n; j++) {
not[i*j]=true;
}
}
return res;
}

[leetcode712]204. Count Primes寻找范围内的素数的更多相关文章

  1. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  2. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  3. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

  4. 【刷题-LeetCode】204. Count Primes

    Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...

  5. 204. Count Primes (Integer)

    Count the number of prime numbers less than a non-negative number, n. 思路:寻找质数的方法 class Solution { pu ...

  6. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  7. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

  8. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  9. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

随机推荐

  1. PP-OCR论文翻译

    译者注: 我有逛豆瓣社区的习惯,因此不经意间会看到一些外文翻译成中文书的评价."书是好书,翻译太臭"."中文版别看"."有能力尽量看原版". ...

  2. 老猿学5G:融合计费的Nchf和Nchf‘服务化接口消息Nchf_ConvergedCharging_Create、Update、Release和Notify

    ☞ ░ 老猿Python博文目录░ 一.引言 在<老猿学5G扫盲贴:中国移动的5G计费架构解读>介绍了5G融合计费的服务化接口包括: CHF提供给CTF使用的Nchf接口 OCF提供给CH ...

  3. 转:python提取浏览器Cookie

    在用浏览器进行网页访问时,会向网页所在的服务器发送http协议的GET或者POST等请求,在请求中除了指定所请求的方法以及URI之外,后面还跟随着一段Request Header.Request He ...

  4. 使用Fiddle修改请求数据

    修改请求数据 以淘宝网为例 命令行中输入bpu 及要拦截的请求地址,如bpu https://s.taobao.com/search 在搜索栏中输入"面包机" 可以看到拦截到的请求 ...

  5. CNVD漏洞证书(2)

    第二张CNVD的原创漏洞证书. 关于证书申请可以看我之前写的这篇博客: https://www.cnblogs.com/Cl0ud/p/12720413.html 继续加油

  6. python——sklearn完整例子整理示范(有监督,逻辑回归范例)(原创)

    sklearn使用方法,包括从制作数据集,拆分数据集,调用模型,保存加载模型,分析结果,可视化结果 1 import pandas as pd 2 import numpy as np 3 from ...

  7. pytorch实战(一)hw1——李宏毅老师作业1

    任务描述:利用前9小时数据,预测第10小时的pm2.5的数值,回归任务 kaggle地址:https://www.kaggle.com/c/ml2020spring-hw1 训练集为: 12个月*20 ...

  8. pycharm 本地代码同步到github上

    第一步, pycharm中setting-> Version Control -> Github -> addacount 如果账号密码登录不成功,就用token登录,参考下面这个博 ...

  9. 【题解】AcWing 193. 算乘方的牛

    原题链接 题目描述 约翰的奶牛希望能够非常快速地计算一个数字的整数幂P(1 <= P <= 20,000)是多少,这需要你的帮助. 在它们计算得到最终结果的过程中只能保留两个工作变量用于中 ...

  10. 题解-CF1401E Divide Square

    题面 CF1401E Divide Square 给一个正方形平面边长为 \(10^6\),给 \(n\) 条横线段和 \(m\) 条竖线段,每条线段都与正方形边缘相交且一条直线上不会有两条线段,求被 ...