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. Educational Codeforces Round 7 A

    Description Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5. ...

  2. css雪碧图制作

    使用css背景合并工具cssSprite 工具下载链接: http://download.csdn.net/download/wx247919365/8741243 1.选择文件 2.生成雪碧图 3. ...

  3. django 母版与继承

    在子页面的页面最上方使用下面的语法来继承母版. {% extends 'layouts.html' %} 块: 通过在母版中使用{% block xxx %}来定义块.在子页面中通过定义母版中的blo ...

  4. Spring boot的热部署

    当把配置文件,比如yml也打到jar包后,如何修改配置,而又不用重新发布呢? 在jar包同一目录下,放置Application.yml (注意,不管jar包内是否此文件名)修改配置文件后,重新启动ja ...

  5. PHP、thinkPHP5.0开发网站文件管理功能(一)显示文件

    显示文件用到的函数有 1.urlencode($str):编码URL字符串,便于将字符串编码并将其用于URL的请求部分 2.urldecode($str):解码已经编码的URL字符串,返回解码后的字符 ...

  6. 自动化构建工具maven

    Maven是目前最流行的自动化构建工具,对于生产环境下多框架.多模块整合开发有重要作用.Maven 是一款在大型项目开发过程中不可或缺的重要工具. 一.什么是构建? 构建并不是创建,创建一个工程并不等 ...

  7. readline的用法

    with open(r'C:\Users\admin\pycdtest\wanyue\llduizhang_20180207\33_1517970821000304388_119061116',enc ...

  8. Unity Screen Screen.currentResolution 当前分辨率

    The current screen resolution (Read Only). 当前屏幕的分辨率.(只读) If the player is running in window mode, th ...

  9. 那些经历过的Bug Unity的Invoke方法

    有一个游戏对象,上面挂着 3 个脚本,如下: using System.Collections; using System.Collections.Generic; using UnityEngine ...

  10. (转)ssh-keygen 中文手册

    ssh-keygen 中文手册 原文:http://www.jinbuguo.com/openssh/ssh-keygen.html 实例:http://blog.csdn.net/yl_1314/a ...