LeetCode 204. Count Primes (质数的个数)

时间:2022-09-25 18:13:47

Description:

Count the number of prime numbers less than a non-negative number, n.


题目标签:Hash Table

  题目给了我们一个n, 让我们找出比n小的 质数的数量。

因为这道题目有时间限定,不能用常规的方法。

首先建立一个boolean[] nums,里面初始值都是false,利用index 和 boolean 的对应,把所有prime number = false;non-prime number = true。

遍历nums array:(从index 2开始,因为1不是prime number)

  只对是false 的数字进行操作:

    1. 首先count,因为 false = prime number;

    2. 把 index(数字) * 2,3,4,....  去到对应的nums[index * 2,3,4...] 的位置里把 non-prime number 的值 改成 true。

      因为是从2 开始的,prime number 只能被1 和 自己所除, 换句话说 prime = prime * 1。所以两个 大于1 的数字 a * b 是不会找到 prime number 的。

Java Solution:

Runtime beats 81.95%

完成日期:05/26/2017

关键词:boolean[] nums

关键点:遍历每一个prime number,利用这个number * 2,3,4... 把之后的non-prime标记出来

 class Solution
{
public int countPrimes(int n)
{
int count = 0;
boolean[] nums = new boolean[n]; // prime = false; non-prime = true for(int i=2; i<n; i++) // iterate from 2 to (n-1)
{
if(!nums[i]) // only access when this num is false
{
count++; // if a num is false meaning this number is prime int m = 2; // multiplier
while(i * m < n) // mark all the non-prime numbers
{
nums[i*m] = true;
m++;
}
}
} return count;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 204. Count Primes (质数的个数)的更多相关文章

  1. &lbrack;LeetCode&rsqb; 204&period; Count Primes 质数的个数

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

  2. &lbrack;leetcode&rsqb; 204&period; Count Primes 统计小于非负整数n的素数的个数

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

  3. &lbrack;LeetCode&rsqb; Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  4. &lbrack;LeetCode&rsqb; 204&period; Count Primes 计数质数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  5. LeetCode 204&period; Count Primes计数质数 &lpar;C&plus;&plus;&rpar;

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

  6. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  7. Leetcode 204 Count Primes 数论

    题意:统计小于n的质数个数. 作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法. 是的,素数筛法并不是该题最佳的解法,线性素数筛法才是. 至于什么是素数筛法,请百度吧. class Sol ...

  8. Java &lbrack;Leetcode 204&rsqb;Count Primes

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

  9. &lbrack;LeetCode&rsqb; 204&period; Count Primes 解题思路

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

随机推荐

  1. 【集合框架】JDK1&period;8源码分析之Collections &amp&semi;&amp&semi; Arrays(十)

    一.前言 整个集合框架的常用类我们已经分析完成了,但是还有两个工具类我们还没有进行分析.可以说,这两个工具类对于我们操作集合时相当有用,下面进行分析. 二.Collections源码分析 2.1 类的 ...

  2. 测试几个xml的问题

    使用sql server的时候,免不了与xml的参数打交道,xml大多数时候都给我们的程序带来方便,但是也有些时候会有变量赋值不通过的时候.(当然罗,如果你本身xml都通不过 xml spy 之类软件 ...

  3. 《BI那点儿事》数据流转换——导入列、导出列

    导入列: 导入列例子现在来做一个例子:创建路径D:\Pictures随便在路径D:\Pictures中粘贴4个比较小的图像文件命名为01.png.02.png.03.png.04.png在路径D:\P ...

  4. 转&colon;TinyXM--优秀的C&plus;&plus; XML解析器

    读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好. TinyXML是一个开源的解 ...

  5. IOS学习之路二十(程序json转换数据的中文字符问题解决)

    ios请求web中的json数据的时候经常出现乱码问题: 例如请求结果可能如下:"\U00e5\U00a5\U00bd\U00e8\U00ae\U00a4" 在网上查到的解决方法是 ...

  6. js操作cookie 使用详解

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp62  JavaScript中的另一个机制:cookie,则可以达到真正全 ...

  7. 使用canvas实现环形进度条

    html代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &lt ...

  8. angular的点击添加

    首先是在js里面我们可以用clone来点击添加一些东西比如列表或者其他的div之类的,但是在angular里面怎么实现点击添加呢? 类似这种: 这样就尴尬了,最少我这样的菜鸟是不知道怎么去写的,网上好 ...

  9. workflow

    一:项目进程 1研发部设计demo (选择方案--方案确认--设计电路图layout--固件开发--软件开发-打样板--调试demo--可靠性分析--稳定性检测) 2进入ES阶段(engineer s ...

  10. &lbrack;NOI导刊2010提高&amp&semi;洛谷P1774&rsqb;最接近神的人 题解(树状数组求逆序对)

    [NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...