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的更多相关文章

  1. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  2. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  3. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  4. [LeetCode] 204. Count Primes 计数质数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  5. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  6. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  7. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  8. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

  9. 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. generator函数

    function* helloWordGenerator() { yield "hello"; yield "world"; return "endi ...

  2. 非ROOT用户启动Tomcat

    [root@Z ~]# adduser tomcat [root@Z ~]# chown -R tomcat:tomcat /usr/local/tomcat/ [root@Z ~]# chown - ...

  3. FAQ unzip无法解压文件

    [root@TEST144239 tmp]# unzip linx64_11gR2_database_1of2.zipArchive:  linx64_11gR2_database_1of2.zipw ...

  4. XAMPP 在windows下无法启动Apache解决方案

    XAMPP 在windows下无法启动Apache解决方案 一.现象 XAMPP 点击Start Apache时出现如下错误 20:41:12  [Apache] Error: Apache shut ...

  5. javascript取得机器名,用户名,读写注册表,启动应用程序

    javascript取得机器名,用户名,读写注册表,启动应用程序//javascript有个特殊的对象ActiveXObject,通过它可以访问windows的本地文件系统和应用程序,比如:有的时候我 ...

  6. Java异常之checked与unchecked

    首先,java的异常分为Error和Exception.这两类都是接口Throwable的子类.Error及Exception及其子类之间的关系,大致可以用下图简述. 注意事项: 1. Error仅在 ...

  7. 按后退键退出Android程序

    主要的目的是按后退键的时候,让程序能够退出. 实现起来的思路: 1.捕获后退键被按了这个事件. Java代码  @Override public boolean onKeyDown(int keyCo ...

  8. C# Color Table颜色对照表

    .AliceBlue 240,248,255 .LightSalmon 255,160,122 .AntiqueWhite 250,235,215 .LightSeaGreen 32,178,170 ...

  9. IAR Embedded Workbench IDE 显示行号

    第一次使用IAR Embedded Workbench IDE,默认设置是不现实代码行号的.显示代码行号方法如下:首先选择“Tools”菜单项,打开“IDE Option”对话框,然后在树状图中选择“ ...

  10. visual studio 声明数组太大,导致栈溢出

    在解释原因前我们先看一下一个由C/C++编译的程序占用的内存分为几个部分: 1.栈区(stack segment):由编译器自动分配释放,存放函数的参数的值,局部变量的值等.在Windows下,栈是向 ...