Description:

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

题目大意:给一个int,返回小于它的质数的数量。

解题思路:打表。

public class Solution {

    public int countPrimes(int n) {
int[] count = new int[n+5];
boolean[] isPrime = new boolean[n+5];
Arrays.fill(isPrime, true);
isPrime[0]=false;
isPrime[1]=false;
for(int i=2;i<=n;i++){
count[i]+=count[i-1];
if(isPrime[i]){
count[i]++;
for(int j =i*2;j<=n;j+=i){
isPrime[j]=false;
}
}
}
return isPrime[n]?count[n]-1:count[n];
}
}

Count Primes ——LeetCode的更多相关文章

  1. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

  2. Count Primes - LeetCode

    examination questions Description: Count the number of prime numbers less than a non-negative number ...

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

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

  4. [leetcode] Count Primes

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

  5. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  6. 【刷题-LeetCode】204. Count Primes

    Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...

  7. LeetCode_204. Count Primes

    204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...

  8. HDU 5901 Count primes 论文题

    Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...

  9. hdu 5901 Count primes (meisell-Lehmer)

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

随机推荐

  1. html获取gps坐标

    <script> function getLocation(){ var options={ enableHighAccuracy:true, maximumAge:1000 } if(n ...

  2. (转)JavaScript判断浏览器类型及版本

    IE 只有IE支持创建ActiveX控件,因此她有一个其他浏览器没有的东西,就是ActiveXObject函数.只要判断window对象存在ActiveXObject函数,就可以明确判断出当前浏览器是 ...

  3. oracle 里面定时执行任务,比如存储过程内容等。

    DECLARE   job_no_ NUMBER;   BEGIN      DBMS_JOB.SUBMIT(job_no_,                   'proc_qszx_dw_sc(' ...

  4. ORA-01653:表空间扩展失败的问题

    以下内容来源于ORA-01653:表空间扩展失败的问题   今天发现,原来设备的数据表空间只有5M,已经满了,上网去找发现要进行扩展空间. 一.脚本修改方式: ----查询表空间使用情况---使用DB ...

  5. 【转】 NSString什么时候用copy,什么时候用strong

    原文: http://blog.csdn.net/itianyi/article/details/9018567 大部分的时候NSString的属性都是copy,那copy与strong的情况下到底有 ...

  6. objectiv-c所有对象之间的交互是如何实现的?

    在对象间交互中每个对象承担不同的角色,总的来说就是“数据发送者”和“数据接收者”两个角色.可以通过objective-c中给我们提供的手段来实现两者间的通讯.比如: “通知中心”NSNotificat ...

  7. Session技术详解

    1.session简介 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务器程 ...

  8. JavaScript HTML DOM 事件

    JavaScript HTML DOM 事件 HTML DOM 使 JavaScript 有能力对 HTML 事件做出反应. 实例 Mouse Over Me 对事件做出反应 我们可以在事件发生时执行 ...

  9. Java多线程:Semaphore

    Semaphore为并发包中提供用于控制某资源同时可以被几个线程访问的类 实例代码: // 允许2个线程同时访问 final Semaphore semaphore = new Semaphore(2 ...

  10. OC之JSON数据解析

    JSON介绍: 作为一种轻量级的数据交换格式,正在逐步取代XML,成为网络数据的通用格式 基于JavaScript的一个子集 易读性略差,编码手写难度大,数据量小 JSON格式取代了XML给网络传输带 ...