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的 ...
随机推荐
- jQuery uploadify上传文件404,500错误
1.如果部署到了IIS7的话,IIS7默认的大小为3000000.修改方法如下: 找到网站双击“请求筛选”——右边找到“编辑功能设置”——将“允许的最大内容长度”改成你想要的就行了. 2.当上传大文件 ...
- Leetcode: Combination Sum IV && Summary: The Key to Solve DP
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- HTML调用servlet(一)
1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...
- nginx、fastCGI、php-fpm关系梳理(转载 http://blog.sina.com.cn/s/blog_6df9fbe30102v57y.html)
前言: Linux下搭建nginx+php+memached(LPMN)的时候,nginx.conf中配需要配置fastCGI,php需要安装 php-fpm扩展并启动php-fpm守护进程, ...
- Android 5.0新特性了解(一)----TabLayout
1.在2015年的google大会上,google发布了新的Android Support Material Design库,里面包含了几个新的控件,其中就有一个TabLayout,它就可以完成Tab ...
- 关于header跳转之后的乱码问题
关于header跳转之后的乱码问题 http://www.360doc.com/content/11/0603/19/7052474_121495648.shtml 问题:不同网站的跳转出现乱码,不同 ...
- paper 30 :libsvm的参数说明
English: libsvm_options: -s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-SVC 2 -- one-c ...
- java mybatis 框架下多种类型的参数传入到xml问题
由于公司要求,最近从.net向java 转,然后过程中遇到各种奇葩问题,特在此随记一番. 场景:一个方法中有两个参数,一个List的集合,一个int 类型的参数,最初我在xml的sql参数,无论定义成 ...
- Python for z/OS
Install pythondev Install DB2 or server driver package easy_install ibm_db Get license file from tor ...
- ubuntu android环境配置
1.下载eclipse 2.下载sdk 3.安装adt 4.配置sdk环境路径sudo gedit /etc/profile export PATH=$JAVA_HOME/bin:$JAVA_HOME ...