(easy)LeetCode 204.Count Primes
Description:
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
解析:大于1的自然数,该自然数能被1和它本身整除,那么该自然数称为素数。
方法一:暴力破解,时间复杂度为O(N^2)
代码如下:
public class Solution {
public int countPrimes(int n) {
if(n<=2) return 0;
int num=0;
for(int i=2;i<n;i++)
if(isPrime(i))
num++;
return num;
}
public boolean isPrime(int i){
for(int j=2;j<i;j++){ //或者for(int j=2;j<=i/2;j++); 或者for(int j=2;j*j<i;j++);
if(i%j==0)
return false;
}
return true;
}
}
运行结果:超时,时间复杂度O(N^2)

方法2:素数的倍数均排除掉。
代码如下:
public class Solution {
public int countPrimes(int n) {
if(n<=2) return 0;
boolean []isPrime=new boolean[n];
int sum=0;
for(int i=2;i<n;i++){
isPrime[i]=true;
}
for(int i=2;i<n;i++){
if(isPrime[i]){
for(int j=2;j*i<n;j++){
isPrime[j*i]=false;
}
}
}
for(int i=2;i<n;i++){
if(isPrime[i]==true)
sum++;
}
return sum;
}
}
运行结果:时间复杂度O(n).

(easy)LeetCode 204.Count Primes的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- [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的质数 ...
- 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. 问题:找出所有小于 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的 ...
随机推荐
- display模版详细介绍
ASP.NET MVC 2 Templates, Part 4: Custom Object Templates Series Index Part 1: Introduction Part 2: M ...
- Activity和Service绑定
Activity和Service绑定后,可以方便Activity随时调用对应的Service里面的方法 绑定代码如下 Activity类代码: <span style="font-si ...
- bash{} 方法总结
假设我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt 我们可以用 ${ } 分别替换获得不同的值: ${file#*/}:拿掉第一条 / 及其左边的字串:dir ...
- [转]使用 Minidumps 和 Visual Studio .NET 进行崩溃后调试
本文关键字:Minidumps, Windows, SEH, VisualC, .NET 摘要 本文讲述了 minidumps 是怎样工作的.当你的程序崩溃的时候应该如何生成它们.以及如何在 Visu ...
- MySQL压缩包安装
1.解压缩 2.添加环境变量 3.添加配置文件 my.ini 4.以管理员身份初始化数据库 mysqld --initialize --user=mysql --console 5.以管理员身份将My ...
- jQuery formValidator使用入门
使用插件必须加载的文件 //加载jQuery类库 <script type="text/javascript" src="jquery-1.7.1.min.js&q ...
- eclipse导入html、js、xml报错的问题
今天重新安装eclipse,在导入部分html.js.xml文件,报错,解决办法如下: eclipse->window->preferences->Team,点击validation ...
- Yii集成smarty说明
1. [在protected目录下建立文件夹vendor/smarty,把smarty的类包放入其中] 2. [在extensions目录下边建立文件CSmarty.php] ...
- memcached应用场景(转)
memcached最吸引人的地方主要在于它的分布式.分布式对于互联网应用来讲,按照用途基本上可划分为三种方式:分布式计算.分布式存储和两者兼而有之.memcached是分布式存储的一种.我们常见的分 ...
- SG函数模板
这篇虽然是转载的,但代码和原文还是有出入,我认为我的代码更好些. 转载自:http://www.cnblogs.com/frog112111/p/3199780.html 最新sg模板: //MAXN ...