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. JavaScript学习笔记之BOM

    BOM的核心对象是window,它既表示浏览器窗口以及页面可见区域,同时也是ECMAScript中的Globe对象,所有的全局变量和函数都是它的属性,并且所有的原声函数以及其他函数也都存在于它的命名空 ...

  2. Lintcode: Interval Sum

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  3. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  4. 转:Jmeter之Bean shell使用(一)

    一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言(这点和JS类似); BeanS ...

  5. 转:python webdriver API 之对话框处理

    页面上弹出的对话框是自动化测试经常会遇到的一个问题:很多情况下对话框是一个 iframe,如上一节中介绍的例子,处理起来稍微有点麻烦:但现在很多前端框架的对话框是 div 形式的,这就让我们的处理变得 ...

  6. PHP判断手机号码是否合法

    <html> <head> <script language="JavaScript"> function checkMobile(input) ...

  7. linux第5天 socket api

    IPv4套接口地址结构通常也称为“网际套接字地址结构”,它以“sockaddr_in”命名,定义在头文件<netinet/in.h>中 通用地址结构用来指定与套接字关联的地址.以socka ...

  8. HTML输入框点击内容消失

    在input标签中这样写 type='text' onfocus='if(this.value=='请输入内容以搜索') this.value=''' onblur='if(this.value==' ...

  9. [Ubuntu] Ubuntu14.04 64bit 编译安装nginx1.7+php5.4+mysql5.6

    我的操作系统是Ubuntu14.04,其它linux系统的操作流程类似. 主要安装的软件是nginx1.7+php5.4+mysql5.6 1. 创建必要目录 sudo mkdir ~/setup s ...

  10. 白盒测试的学习之路----(四)搭建测试框架TestNG测试

    TestNG是一个开源自动化测试框架; TestNG是类似于JUnit,但它不是一个JUnit扩展.它的灵感来源于JUnit.它的目的是优于JUnit的,尤其是当测试集成的类. TestNG消除了大部 ...