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. 数据结构线性表(js实现)

    最近在复习数据结构的过程中,发现基本上数据结构都是用C来实现的,自己之前学习的时候也是用C去写的,由于目前对js更为熟悉一些,所以这里选择使用js去实现其中的某些算法和结构.实际上算法和语言关系不大, ...

  2. nginx 一般网站部署常用参数设置

    #user nobody;worker_processes 2; #error_log logs/error.log;#error_log logs/error.log notice;#error_l ...

  3. CentOS快速搭建subversion服务器

    环境:centos x86_64说明:使用daemon的方式来跑svn,不使用apache首先到:http://subversion.tigris.org/ 下载最新的subversion源码 我下的 ...

  4. C语言strlen函数和sizeof操作符

    字符'x'于字符串"x"的区别 'x' 属于基本类型(char)字符类型-----------------由1个字符组成('x') "x"属于派生类型(char ...

  5. MySQL错误: could not retrieve transation read-only status server

    问题描述: java代码在开始事务后,先做了一个查询,再insert,此时会报:          java.sql.SQLException: could not retrieve transati ...

  6. Perl 模块 Getopt::Std 和 Getopt::Long

    示例程序: getopt.pl; 1 2 3 4 5 6 7 8 #!/usr/bin/perl -w #use strict; use Getopt::Std; use vars qw($opt_a ...

  7. Redis应用案例,查找某个值的范围(转)

    本文来自Redis在Google Group上的一个问题,有一位同学发贴求助,说要解决如下的一个问题:他有一个IP范围对应地址的列表,现在需要给出一个IP的情况下,迅速的查找到这个IP在哪个范围,也就 ...

  8. Eclipse中Maven+Spring3.2.8+SpringMVC HelloWorld项目

    本文适合有一定spring和springmvc基础,并想使用Maven管理项目的人. 源码打包:http://pan.baidu.com/s/1hqurUcs 转载请声明出处(http://www.c ...

  9. RedisCacheTool参考其中的文件读写功能

    package com.jr.market.tool; import java.io.BufferedReader; import java.io.File; import java.io.FileI ...

  10. sql中count(*)和count(字段名)区别

    数据测试:见图 sql 语句见分晓: SELECT COUNT(*) c1 ,COUNT(ADDRESS) c2 FROM test 显而易见,count(*)统计的是结果集的总条数,count(字段 ...