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. jquery知识点结合使用

    1.在页面的某个具有id的element内插入其他element,并给其增加悬浮提示. $('#id').after('<div id='box'></div>'); $('# ...

  2. 启用NFS方案(读写分离)

  3. 17.JavaMail

    1.电子邮件 电子邮件是目前网络上使用最多的服务,电子邮件的应用越来越广泛正常的通信往来账号注册时,找回密码时等一般发送的邮件主要可以分解成2大部分一部分是发信人.接信人.主题等邮件标头另外一部分是邮 ...

  4. 问题解决Determine IP information for eth0.. no link present check cable

    网上方法都没有解决:简单粗暴编辑里还原了默认设置OK了 网上方法1 一般解决办法: 第一步: 到/etc/sysconfig/network-scripts/ifcfg-eth<n>/et ...

  5. my24_mysql索引-使用篇

    索引提示 SELECT * FROM table1 USE INDEX (col1_index,col2_index) ; SELECT * FROM table1 IGNORE INDEX (col ...

  6. vue-cli构建的项目打包出现里面的js,css缺少dist路径

    转载 https://www.cnblogs.com/wanf/p/7871787.html

  7. django-filter version 2.0 改动

    今天使用django-filter时候遇到了下面这个问题: django-filter: TypeError at /goods/ init() got an unexpected keyword a ...

  8. C语言有GetOpenFile吗?

    windows中有个GetOpenFile的函数,就是上面那个啦!!我们怎么来实现呢?要我用C语言写出来我真的跪了..但是我们可以输入文件的[绝对路径],配合fopen函数来实现的.. 注意问题 ①文 ...

  9. android shape.xml 属性详解

    转载源:http://blog.csdn.net/harvic880925/article/details/41850723 一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标 ...

  10. xubuntu 安装一款漂亮的图标

    sudo add-apt-repository ppa:noobslab/icons sudo apt-get update sudo apt-get install square-beam-icon ...