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. [LeetCode] 204. Count Primes 质数的个数

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

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

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

  3. [LeetCode] Count Primes 质数的个数

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

  4. [LeetCode] 204. Count Primes 计数质数

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

  5. LeetCode 204. Count Primes计数质数 (C++)

    题目: 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 [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. 【漏洞公告】CVE-2017-12615/CVE-2017-12616:Tomcat信息泄漏和远程代码执行漏洞

    2017年9月19日,Apache Tomcat官方确认并修复了两个高危漏洞,漏洞CVE编号:CVE-2017-12615和CVE-2017-12616,该漏洞受影响版本为7.0-7.80之间,在一定 ...

  2. 使用Spring的隐式注解和装配以及使用SpringTest框架

    SpringTestConfiguration 1.加入jar 包spring-test-4.3.9.RELEASE.jar 2.写基本的Component 注意级联状态下  需要给需要调用的属性加入 ...

  3. 在 docker 容器中捕获信号

    我们可能都使用过 docker stop 命令来停止正在运行的容器,有时可能会使用 docker kill 命令强行关闭容器或者把某个信号传递给容器中的进程.这些操作的本质都是通过从主机向容器发送信号 ...

  4. Highway Networks Pytorch

    导读 本文讨论了深层神经网络训练困难的原因以及如何使用Highway Networks去解决深层神经网络训练的困难,并且在pytorch上实现了Highway Networks. 一 .Highway ...

  5. oracle 行转列 列转行

    行转列 这是一个Oracle的列转行函数:LISTAGG() 先看示例代码: with temp as( select 'China' nation ,'Guangzhou' city from du ...

  6. .Neter玩转Linux系列之二:Linux下的文件目录及文件目录的权限

    一.Linux下的文件目录 简介:linux的文件系统是采用级层式的树状目录结构,在此 结构中的最上层是根目录“/”,然后在此目录下再创建 其他的目录.深刻理解linux文件目录是非常重要的,如下图所 ...

  7. Distribute Candies

    Given an integer array with even length, where different numbers in this array represent different k ...

  8. Linux CentOS 7 防火墙/端口设置

    CentOS升级到7之后用firewall代替了iptables来设置Linux端口, 下面是具体的设置方法: []:选填 <>:必填 [<zone>]:作用域(block.d ...

  9. python contextlib 上下文管理器

    1.with操作符 在python中读写文件,可能需要这样的代码 try-finally读写文件 file_text = None try: file_text = open('./text', 'r ...

  10. 使用Fabric一键批量部署上线/线上环境监控

    本文讲述如何使用fabric进行批量部署上线的功能 这个功能对于小应用,可以避免开发部署上线的平台,或者使用linux expect开发不优雅的代码. 前提条件: 1.运行fabric脚本的机器和其他 ...