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

Example:

  1. Input: 10
  2. Output: 4
  3. 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.

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

Solution 2:  is prime function

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

Example:

  1. Input: 10
  2. Output: 4
  3. 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. 求js数组的最大值和最小值

    数组 ,,,,,,,,,]; 方法1 - 字符串拼接法 利用toString或join把数组转换为字符串,再和Math的max和min方法分别进行拼接,最后执行eval方法 var max = eva ...

  2. hdu-4513吉哥系列故事——完美队形II--最长回文

    吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  3. Android RecyView 滑动置指定位置

    1,直接回到顶部 recyview.getLinearLayoutManager().scrollToPositionWithOffset(0, 0); 2,慢慢的回到顶部 private void ...

  4. Photoshop入门教程(三):图层

    学习心得:图层可以说是Photoshop的核心,看似简单,但是对于图像的各种编辑都是基于图层.他就像一层透明的.没有厚度的玻璃纸,每张玻璃纸设置不同的效果,层层叠加,最后显现出绚烂的效果. 在进行图像 ...

  5. mybatis用法

    转载:https://www.cnblogs.com/xdp-gacl/p/4261895.html 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架 ...

  6. 内置函数_eval

    eval功能:将字符串str当成有效的表达式来求值并返回计算结果. 语法: eval(source[, globals[, locals]]) -> value 参数说明: expression ...

  7. 关于WampServer一些配置修改

    1.解决WAMP mysql中文乱码问题(在mysql的my.ini文件中) 1).找到client字段并添加:default-character-set=utf8 2).找到mysql字段并添加: ...

  8. mysql 安装以及卸载 CentOS 7

    wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar 安装:mkdir ...

  9. firewall 端口转发

    centos 7 使用背景:某次新购阿里云服务器安装nginx后配置80转8080的内部转发 systemctl status firewalld ---查看守护进程状态systemctl start ...

  10. shiro【filter】

    alt+7 OncePerRequestFilter public final void doFilter(ServletRequest request, ServletResponse respon ...