(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的 ...
随机推荐
- mongodb 最佳实践
MongoDB功能预览:http://pan.baidu.com/s/1k2UfW MongoDB在赶集网的应用:http://pan.baidu.com/s/1bngxgLp MongoDB在京东的 ...
- dubbo Linux 解决:nc: command not found
出现该情况有两种可能: (1)没有配置nc命令的环境变量: (2)该系统没有安装nc命令: 我查看了一下在/usr/bin目录中并没有nc命令,所以我可以认为出现该情况的原因是第二种情况 下载安装 下 ...
- oracle11gRAC环境使用RMAN增量备份方案
转摘:http://blog.itpub.net/29819001/viewspace-1320977/ [oracle@zx ~]$ rman target /Recovery Manager: R ...
- ajax同步,异步
传输数据 同步:第一条传过去,对方接收到反馈以后,再传第二条; 异步:第一条传过去,不管对方有没有收到,继续传第二条. ajax(默认异步 async:true) 同步:执行完ajax内部代码,才执行 ...
- jQuery validate在没有校验通过的情况下拒绝提交
下面通过一个简单的例子说明,这个问题,可能是很多人遇到的,验证不通过的时候,依然提交了表单. HTML <form class="survey" id="surve ...
- [原]在Fedora中编译Libevent测试实例
在我的昨天的博文<[原]我在Windows环境下的首个Libevent测试实例>中介绍了在Windows环境下如何编译一个echo server例子.今天我又试了一下在Linux环境中编译 ...
- Hibernate 抓取策略fetch-2 (批量抓取batch-size以及hibernate.jdbc.fetch_size、hibernate.jdbc.batch_size)
类关系: User N~1 Group 测试代码: System.out.println("1"); List stuList = session.createQuery(&quo ...
- 怎么利用WinPE恢复系统注册表?
我们的电脑总是会遇到各种各样的问题,最好用的方式就是电脑重装,重装系统的方式有很多,光盘安装.硬盘安装.U盘安装等.但是碰到电脑系统瘫痪无法启动,甚至连安全模式也进不了的时候,你的光盘.硬盘就没有用处 ...
- (转)颜色渐变CSS
本文转载自:http://www.cnblogs.com/yichengbo/archive/2012/10/27/2742618.html IE系列 filter: progid:DXImageTr ...
- android学习笔记三
GUI==>Graphics User Interface,图形用户界面. android UI 建立在View.ViewGroup基础上,采用组合器设计模式设计View和ViewGoup. V ...