*204. Count Primes (siecing prime)
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)的更多相关文章
- 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 ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- 204. Count Primes - LeetCode
Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- 【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- 204. Count Primes (Integer)
Count the number of prime numbers less than a non-negative number, n. 思路:寻找质数的方法 class Solution { pu ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
随机推荐
- poj3020 建信号塔(匈牙利算法 最小覆盖边集)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10518 Accepted: 518 ...
- input只能输入非负数
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Sqlite和mysql的区别及优缺点
小型嵌入式,跟mysql差不多,但更小,功能也少,属于本地数据库多用于读多写少,100W以下的数据压力不大,不支持分布式. 真正开源的小型嵌入式数据库,支持事务.触发器器 性能至少优于mysql ...
- 9 Strings
1 Strings 1.1 Strings and GStrings Groovy允许你使用2种不同类型的字符串类型,分别是java.lang.String和groovy.lang.GS ...
- mgo02_mongodb启动警告处理
** WARNING: Access control is not enabled for the database. 原因分析:新版本的MongDB增加了安全性设计,推荐用户创建使用数据库时进行验证 ...
- VSCode创建自定义用户片段
1.选择相应的用户片段类型(以"Java"为例) 首选项 -> 用户代码片段 -> java 2.设置模板 prefix 触发快捷提示的字符串前缀 body 代码片段主 ...
- Silverlight 动态创建Enum
private Type CreateEnum() { List<string> lists = new List<string>(); lists.Add("男&q ...
- Tree UVA - 548(二叉树递归遍历)
题目链接:https://vjudge.net/problem/UVA-548 题目大意:给一颗点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序遍历和后序遍历,找一个叶子结点使得它到根 ...
- Ubuntu 16.04系统上NFS的安装与使用
摘要:本文介绍了NFS服务器的安装过程.配置文件和常用命令行工具,以及NFS客户端上如何安装常用工具,介绍如何挂载共享目录,并通过实验进行验证. 一.服务器端: 1.1安装NFS服务: #执行以下命令 ...
- Python3实现计算BMI指数,跟我一起来计算你的BMI吧
废话不多说,直接上程序哈: name=input('Name:') height=input('Height(m):') weight=input('Weight(kg):') BMI=float(f ...