LeetCode----204. Count Primes(Java)
package countPrimes204;
/*
* Description:
* Count the number of prime numbers less than a non-negative number, n.
*/
public class Solution {
//Time Limit Exceeded
/*
public static int countPrimes(int n) {
int number=0;
for (int i=0;i<n;i++)
if(IsPrime(i)==true)
number++;
return number;
}
public static boolean IsPrime(int n){
if (n <= 3)
return n > 1;
else if (n%2==0||n%3==0)
return false;
else{
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i == 0)
return false;
}
}
return true;
}
*/ public static int countPrimes(int n) {
//boolean default is false
boolean[] IsPrime=new boolean[n];
int numPrime=0;
for (int i=2;i<n;i++){
if (IsPrime[i-1]==false){
numPrime++;
for(int j=2;i*j<n;j++)
IsPrime[i*j-1]=true;
}
}
return numPrime;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(countPrimes(15000000));
boolean[] IsPrime=new boolean[2];
System.out.println(IsPrime[0]);
}
//reference
public int countPrimes2(int n) {
boolean[] notPrime = new boolean[n];
int count = 0;
for (int i = 2; i < n; i++) {
if (notPrime[i] == false) {
count++;
for (int j = 2; i*j < n; j++) {
notPrime[i*j] = true;
}
}
}
return count;
}
}
LeetCode----204. Count Primes(Java)的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- Java for LeetCode 204 Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...
- [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 click to show more ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- (easy)LeetCode 204.Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...
- [LeetCode] 204. Count Primes 解题思路
Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...
- LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes
原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...
随机推荐
- MVC 登录认证与授权及读取登录错误码
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 最近在自学MVC,遇到的问题很多,索性一点点总结下 ...
- 集群因子(Clustering Factor)
clustering factor是CBO使用的统计信息,用来衡量一个表中的列是否是规则排序存放的. 在通过索引访问表的时候,被用来作为代价评估的指示器.扫描索引的时候,clustering fact ...
- 数据存储之plist、偏好设置
// 偏好设置--------------------------------- // 存储基本类型数据 NSUserDefaults *defaults = [NSUserDefaults stan ...
- Java基础之线程——管理线程同步方法(BankOperation2)
控制台程序. 当两个或多个线程共享同一资源时,例如文件或内存块,就需要采取措施,确保其中的一个线程不会修改另一个线程正在使用的资源.当其中的一个线程更新文件中的某个记录,同时另一个线程正在检索这个记录 ...
- 编写简单的Mapreduce程序并部署在Hadoop2.2.0上运行
今天主要来说说怎么在Hadoop2.2.0分布式上面运行写好的 Mapreduce 程序. 可以在eclipse写好程序,export或用fatjar打包成jar文件. 先给出这个程序所依赖的Mave ...
- java collections读书笔记(9)collection框架总览(2)
框架算法: 1)collection接口 add() Adds an element to the collection.addAll() Adds a collection of element ...
- mysql查找字符串出现位置
MySQL中的LOCATE和POSITION函数使用方法 FIND_IN_SET(str,strlist) 假如字符串str 在由N 子链组成的字符串列表strlist 中,则返回值的范围在 1 到 ...
- spring AutowireCapableBeanFactory 自动注入
文档:http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/Auto ...
- How about xlogs are missing and xlogs are deleted
[postgres@minion1 bin]$ pwd /usr/local/pgtest/bin [postgres@minion1 bin]$ ./pg_ctl -D ../data/ start ...
- Struts2配置文件各种标签的含义
最近正在学习Struts2,在配置文件中遇到好多标签,各种意义不同.为了方便学习,便把各种标签的书写和含义总结如下:(随时更新) <struts> <!-- 开启使用开发 ...