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. JS中有关分支结构、循环结构以及函数应用的一些简单练习

    案例一:搬桌子    年龄大于七岁男女都可以搬桌子,年龄小于七岁大于五岁的男生可以搬桌子: var num =parseInt(prompt("请输入你的年龄")) var sex ...

  2. php使用ZipArchive压缩文件的心得

    $zip=new ZipArchive; if($zip->open('test.zip',ZipArchive::CREATE)===TRUE){ $zip->addFile('imag ...

  3. response对象的使用

    使用response对象提供的sendRedirect()方法可以将网页重定向到另一个页面.重定向操作支持将地址重定向到不同的主机上,这一点与转发是不同的.在客户端浏览器上将会得到跳转地址,并重新发送 ...

  4. iOS多线程编程

    废话不多说,直接上干货.先熟悉一下基本知识,然后讲一下常用的两种,NSOperation和GCD. 一.基础概念 进程: 狭义定义:进程是正在运行的程序的实例(an instance of a com ...

  5. CSS3的颜色渐变效果

    在 animate.css寻找自己想要的动态效果,看到标题Animate.css和按钮Animate it的颜色在逐渐变化,觉得蛮有趣的,把控制变化的相关代码扒了下来,自己分析实现一波. 一开始认为使 ...

  6. 推荐一个比FiddlerCore好用的HTTP(S)代理服务器

    为什么不用FiddlerCore? 说到FiddlerCore大家可能会比较陌生,那么它哥Fiddler就比较熟悉了:抓包.模拟低带宽.修改请求我平时比较常用.Fiddler的本质就是一个HTTP代理 ...

  7. 逆向实战干货,快速定位自动捡阳光Call,或者标志

    逆向实战干货,快速定位自动捡阳光Call,或者标志 注意: 关于CE和OD的使用,这里不再多说,快速定位,默认大家已经有了CE基础,或者OD基础. 第一种方法,找Call 第一步,打开CE,搜索阳光值 ...

  8. C语言编译过程及数据类型

    写在前面 C语言可以称得上是高级语言中的低级语言,接下来一段时间,我会写一下文章关于c语言,把它的神秘面纱一 一揭开.下面主要是c语言的C语言编译过程及数据类型 源文件编译过程 为了使计算机能执行高级 ...

  9. java 面向对象 2

    一.JAVA类的定义 JAVA里面有class关键字定义一个类,后面加上自定义的类名即可.如这里定义的person类,使用class person定义了一个person类,然后在person这个类的类 ...

  10. 一个基于Asp.net MVC的博客类网站开源了!

    背景说明: 大学时毕业设计作品,一直闲置在硬盘了,倒想着不如开源出来,也许会对一些人有帮助呢,而且个人觉得这个网站做得还是不错了,毕竟是花了不少心思,希望对你有所帮助. github地址:https: ...