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. 模拟实现一个ATM+购物商城程序

    记得上次小编上传了一个购物车程序,这次呢稍微复杂一点,还是那句话,上传在这里不是为了炫耀什么,只是督促小编学习,如果大神有什么意见和建议,欢迎指导.(PS:本次主要参考学习为主,自己原创的很少) 要求 ...

  2. Mybatis第九篇【基于Maven在Idea下Mybatis逆向工程】

    前言 在Intellij idea下,没有学习Maven的情况下使用Mybatis的逆向工程好像有点复杂,资料太少了-找到的资料好像也行不通- 于是学完Maven之后,我就再来更新Idea下使用Myb ...

  3. MongoDB 所支持的数据类型 创建和删除集合 创建和删除数据库

    数据类型 MongoDB 支持如下数据类型: String:字符串.存储数据常用的数据类型.在 MongoDB 中,UTF-8 编码的字符串才是合法的. Integer:整型数值.用于存储数值.根据你 ...

  4. MySQL的一点浅显知识

    本人最近看了一本有关于MySQL的书籍<MySQL必知必会>,书中只写了一些基本知识,但是也基本涵盖了所有的MySQL的知识点.其余的比较高级的也只是在基础上进行扩展或者是优化,看完这本书 ...

  5. ios开发——实用技术篇&三维旋转动画

    实现三位旋转动画的方法有很多种,这里介绍三种 一:UIView 1 [UIView animateWithDuration:1.0 animations:^{ 2 self.iconView.laye ...

  6. Linux 文件查找

    在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 whereis wher ...

  7. PHP实现页面静态化

    1.通过buffer来实现   需要用file_put_contents ob_get_clean()等内置函数   ob_start (); include "filterpost.htm ...

  8. 变量的声明和定义以及extern的用法

    变量的声明和定义以及extern的用法                                          变量的声明不同于变量的定义,这一点往往容易让人混淆. l         变量 ...

  9. jQuery经典案例

    示例1:鼠标点击左侧菜单实现打开和关闭功能: html及css代码部分: <!DOCTYPE html> <html lang="en"> <head ...

  10. RobotFramework自动化测试框架-移动手机自动化测试Click A Point关键字的使用

    Click A Point关键字用来模拟点击APP界面上的一个点,该关键字接收两个三个参数[ x=0 | y=0 | duration=100 ],x和y代表的是点的坐标位置,duration代表的是 ...