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)的更多相关文章

  1. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  2. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  3. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  4. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  5. [LeetCode] 204. Count Primes 计数质数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  6. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  7. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  8. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

  9. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

  10. 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的 ...

随机推荐

  1. oracle启动关闭命令

    关闭:1.shutdown normal 不允许新的连接.等待会话结束.等待事务结束.做一个检查点并关闭数据文件.启动时不需要实例恢复. 2.shutdown transactional不允许新的连接 ...

  2. example of Python http server

    from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler print "hello " class TestHTT ...

  3. ios-消息弹框之UIAlertView, UIActionSheet以及UIAlertController小结

    首先storyboard中创建对应按钮并拖线,来演示不同的效果 首先点击了actionSheet按钮效果如图 实现弹框需要遵守设置代理,遵守协议. 效果就是从底部向上弹起来的框框. 通过对按钮的点击输 ...

  4. 手动purge优化器的统计信息与AWR快照,减少对sysaux表空间的占用

    1.运行以下脚本,计算当前优化器统计信息和AWR快照表占用sysaux的空间 SQL> conn / as sysdba SQL> @?/rdbms/admin/awrinfo.sql 2 ...

  5. SQLSERVER:通过sys.tables实现批量删表、快速统计多表记录和

    SQLSERVER:通过sys.tables实现批量删表,或者回滚表 begin try drop table #temp10 end try begin catch end catch select ...

  6. SqlServer2008根据现有表,获取该表的分区创建脚本

    *============================================================== 名称: [GetMSSQLTableScript] 功能: 获取cust ...

  7. Git_Commands

  8. MAXFLOAT

    CGSizeMake(300, MAXFLOAT),是计算宽和高的,里面的MAXFLOAT通俗点说就是最大的数值,代表你的label的宽和高是随着你label内容而变化,不用担心因为label内容过长 ...

  9. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  10. JAVA-面向对象-特性

    1.封装 1.定义方式 1修饰符class类名 2类名首字母大写 2.类的成员 1属性 成员变量 可以设置默认值 第一个单词首字母小写,后面首字母大写 一般把属性设置成private 提供属性对应的g ...