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. 聊聊JAVA中 String类为什么不可变

    前言 "我的风格比较偏传统和经典" 小明说,"我们在打扮自己的问题上还是蛮冒险的...我觉得当你是只狗的时候,穿什么都hold的住!" 哈哈哈,脱离单身狗快两年 ...

  2. 二分求最长上升子序列 二分LIS

    #include <iostream> #include <cstring> #define N 50010 using namespace std; int n; int n ...

  3. Shiro第一篇【Shiro的基础知识、回顾URL拦截】

    Shiro基础知识 在学习Shiro这个框架之前,首先我们要先了解Shiro需要的基础知识:权限管理 什么是权限管理? 只要有用户参与的系统一般都要有权限管理,权限管理实现对用户访问系统的控制,按照安 ...

  4. 简单CSS 布局

    CSS Layout CSS Layout 是对上下左右布局的一个简单封装,主要针对自己项目里面方便使用. 坚持组合大于继承的原则,复杂的布局也是由简单布局组成的. 所以不习惯margin/paddi ...

  5. oracle sql 树操作

    语法:select-start with-connect by-prior 主要有两点 1)prior放在子节点端,则表示扫描树是以start with指定的节点作为根节点从上往下扫描.可能对应一个或 ...

  6. 为什么要用深度学习来做个性化推荐 CTR 预估

    欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:苏博览 深度学习应该这一两年计算机圈子里最热的一个词了.基于深度学习,工程师们在图像,语音,NLP等领域都取得了令人振奋的进展.而深 ...

  7. JS组件系列——基于Bootstrap Ace模板的菜单Tab页效果优化

    前言:之前发表过一篇  JS组件系列——基于Bootstrap Ace模板的菜单和Tab页效果分享(你值得拥有) ,收到很多园友的反馈,当然也包括很多诟病,因为上篇只是将功能实现了,很多细节都没有处理 ...

  8. Find The Multiple (poj1426 一个好的做法)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16505   Accepted: 673 ...

  9. .Neter玩转Linux系列之一:初识Linux

    一.为什么要学习Linux (1)首先我们欣赏一下,曾经的微软是如何看待Linux的,是不是很惊讶,微软还是很可爱的(#^.^#) 如今的微软看待Linux的态度:有人说微软技术那么厉害,难道微软就不 ...

  10. validators配置要点及No result defined for action报错解决方案

    在做JavaEE SSH项目时,接触到validators验证. 需要了解validators配置,或者遇到No result defined for action 这个错误时,可查阅本文得到有效解决 ...