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

Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

Solution2: ssieving: need a helping array with false initialization: false(prime) true(non- prime), inner loop: only set the multiple of prime.

class Solution {
int res = 0;
//solution 1: fun: check each unmber n * (n)
//seieve solution: 2,3,5,7,11,13
public int countPrimes(int n) {
// 2: 1 3: 2 4: 2 ,5 :3 ....
boolean[] aux = new boolean[n+1]; //all false (prime)
for(int i = 2; i <n; i++){//i: number in n
if(aux[i] == false) res++;
if(aux[i] == true) continue; //pass the non -prime question: 2 and 4 have the same multiple??
//sieving the number is multiple of this target nuber : aux[i]
for(int j = 2; j*i <n; j++){
aux[j*i] = true;
}
}
return res;
}
//time: i = 2 :n/2
// i = 3 : n/3 or smaller
// sum(n/i) : i is prime : n*sum(1/i) = n*(lg(lgn))
}

Solution 2:  is prime function

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

Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

*204. Count Primes (siecing prime)的更多相关文章

  1. 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 ...

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

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

  3. 204. Count Primes - LeetCode

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

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

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

  5. [LeetCode] 204. Count Primes 质数的个数

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

  6. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

  7. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  8. 204. Count Primes (Integer)

    Count the number of prime numbers less than a non-negative number, n. 思路:寻找质数的方法 class Solution { pu ...

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

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

随机推荐

  1. hdu Minimum Inversion Number(逆序数的小知识与线段树)

    飞! 题解 首先,求逆序数对的思路: 1.得到整个数列后,从前往后扫,统计比a[i]小的,在a[i]后面的有多少个 这样做的话,应该是只有n2的暴力作法,没想到更好的方法 2.统计a[i]前面的,且比 ...

  2. navicat data modeler的使用以及数据库设计的流程

    E-R图(Entity Relationship Diagram) 又称实体-联系图 (提供了表示实体类型.属性和联系的方法,用来描述现实世界的概念模型) 构成E-R图的3个基本要素是实体型.属性和联 ...

  3. TensorFlow入门测试程序

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist=input_data. ...

  4. 在Pycharm上编写WordCount程序

    本篇博客将给大家介绍怎么在PyCharm上编写运行WordCount程序. 第一步 下载安装PyCharm 下载Pycharm PyCharm的下载地址(Linux版本).下载完成后你将得到一个名叫: ...

  5. vue(1)安装

    1.安装node.js(https://nodejs.org/en/),我安装的是 v10.15.1 1).在nodejs安装路径下,新建node_global和node_cache两个文件夹 2). ...

  6. ES6数组新增方法总结

    关于数组中forEach() .map().filter().reduce().some().every()的总结 let arr = [1, 2, 3, 4, 5] // forEach遍历数组 a ...

  7. hdfs shell命令及java客户端编写

    一. hdfs shell命令 可以通过hadoop fs 查看所有的shell命令及其用法. 传文件到hdfs: hadoop fs -put /home/koushengrui/Downloads ...

  8. HTML6即将到来,你没有看错是HTML6

    HTML6提案1:对视频对象的更多控制 我们可能永远不会解决与压缩编解码器的争斗,但我们可以与之配合.不同的压缩算法可能需要更多的工作来实现,但是它们提供竞争.能对展现在页面上的视频帧提供更多控制的方 ...

  9. auto uninstaller 简体中文版 更新下载地址

    地址一(腾讯微云) 地址二(百度网盘)    提取码:3nx7 地址三(直接下载)

  10. pat1100. Mars Numbers (20)

    1100. Mars Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue People o ...