LeetCode中Count Primes的java实现
来源:互联网 发布:java web项目开发案例 编辑:程序博客网 时间:2024/06/06 03:04
题目如下:
Description:
Count the number of prime numbers less than a non-negative number, n.
一开始用的方法入下,结果运算时间太大
public class Solution {public int countPrimes(int n) {
boolean mark = true;
int count=0;
for(int i=2;i<n;i++)
{
for(int m=2; m<=Math.sqrt(i);m++ )
{
mark = true;
if(i%m==0)
{
mark =false;
break;
}
}
if(mark)
{
count++;
}
}
return count;
}
}
后来改成如下写法,通过
public class Solution {
public int countPrimes1(int n)
{
boolean bool[] = new boolean[n];
if (n <= 2) return 0;
int counter = n-2;
int j = 0;
for (int i = 2; i <= Math.floor(Math.sqrt(n)); i++) {
j = i + i;
if (!bool[i])
{
while (j < n) {
if (!bool[j]) {
bool[j] = true;
counter --;
}
j = j + i;
}
}
}
return counter;
}
- LeetCode中Count Primes的java实现
- Count Primes Leetcode JAVA
- [LeetCode] Count Primes(Java)
- Leetcode:204. Count Primes(JAVA)
- leetcode最近的一题:count primes
- LeetCode 204 - Count Primes
- [LeetCode] Count Primes
- LeetCode #Count Primes#
- 【leetcode】Count Primes
- LeetCode 204-Count Primes
- [LeetCode] Count Primes
- [LeetCode 204] Count Primes
- [LeetCode]Count Primes
- leetcode 204: Count Primes
- LeetCode OJ Count Primes
- [leetcode]Count Primes
- leetcode--Count Primes
- [LeetCode]Count Primes
- Spring+Hibernate Error: org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested
- LCM的个数 UVa10892
- Oracle 备份脚本
- linux的tar命令压缩和解压缩文件
- SAP中负数的表示增强 负号从数字后更改到数字前面
- LeetCode中Count Primes的java实现
- 4.1 eclipse -android 中文乱码的解决
- Logstash
- Oracle LISTAGG排重方法
- JAVA反射机制
- ios如何通过storyboard获得视图控制器
- 如何高效的将word文档转换成PDF文档
- 文件操作设定路径的一些注意
- BZOJ 1123: [POI2008]BLO