1 题目

Description:

Count the number of prime numbers less than a non-negative number, n.

接口:public int countPrimes(int n);

2 思路

统计小于n的素数个数,注意不包括n。

思路1:素数的判断

很容易想到素数的判断isPrime,然后逐个数来进行判断是否是素数。进行统计,输出结果。

复杂度: 把isPrime时间复杂度控制在O(n^0.5)的话,因此:Time:O(n^1.5) , Space:O(1)。

提交代码仍然超时。

思路2: 素数表

素数表的产生,在[1 to n)的范围内,标记出 非素数,剩下的就是素数了。

思路:

  1. 初始化所有的[2,n)都是素数
  2. 剔除掉 非素数
  3. 统计素数个数

如何标记 非素数呢?分组标记:

  • 2,[4,6,8,10,12,14,16,18,20,22,24...];
  • 3,[9,12,15,18,21,24,27,...];
  • 5,[25,30,35...]
  • 考虑一下终止条件

复杂度: Time: O(n log log n) , Space: O(n)

3 代码

思路1

	// 判断一个数是否是素数的方法:不是最好,但还可以。素数表是判断素数的好方法。
// Time:O(sqrt(n)) Space:O(1)
boolean isPrime(int n) {
for (int i = 2; (i * i) <= n; i++) {
if (n % i == 0)
return false;
}
return true;
} /**
* Solution 1: 逐个判断是否是素数,思路简单。但是超时
* Time:O(n^1.5) Space:O(1)
*/
public int countPrimes(int n) {
int count = 0;
for (int i = 2; i <= n; i++) {
if(isPrime(i)) count++;
}
return count;
}

思路2

	// Time: O(n log log n) Space: O(n)
public int countPrimes(int n) {
// 初始化所有的都是素数,在剔除掉 `非素数`
boolean[] isPrime = new boolean[n];
for (int i = 2; i < n; i++) {
isPrime[i] = true;
} // 剔除非素数
for (int i = 2; (i * i) < n; i++){
if (isPrime[i]) {
for (int j = i * i; j < n; j += i) {
isPrime[j] = false;
}
}
} // 统计素数个数
int count = 0;
for (boolean is : isPrime) {
if (is) count++;
}
return count;
}

4 总结

素数是比较经典的题目。此题的tag: HashTable, Math

5 参考

leetcode面试准备: CountPrimes的更多相关文章

  1. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  2. leetcode面试准备: Game of Life

    leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...

  3. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  4. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  5. leetcode面试准备:Reverse Words in a String

    leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...

  6. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  7. leetcode面试准备:Triangle

    leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...

  8. leetcode面试准备:Sliding Window Maximum

    leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...

  9. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

随机推荐

  1. 万网免费主机wordpress快速建站教程-wordpress下载及安装

    进入wordpress官网(http://cn.wordpress.org)下载最新的wordpress安装程序,下载完成后解压到任意电脑目录. 解压完毕后,使用FTP管理工具上传安装文件至主机htd ...

  2. Linq常用查询运算符

    Linq一共包含五十几个查询运算符,常用的根据类型来区分一共有5类左右,这五类里面一些事在项目查询中经常用到的.不过linq运算符的命名十分规范,基本从字面意思就能猜测出来是干嘛用的,下面我们挑选一些 ...

  3. Solr配置与简单Demo

    简介: solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目.它的官方网址在http://lucene.apache.org/solr/  .solr需要运行在 ...

  4. Swift 语法须知

    什么是swift? swift是 2014 WWDC 发布的一款脚本语言. 使用Swift的好处: OC ARC    最大的困难  内存管理 而  swift  不用担心内存方面.   简洁 ,功能 ...

  5. 利用putty实现文件在linux上传和下载

    利用putty实现文件上传和下载:1.打开windows命令提示符窗口d:(putty在d盘下)cd putty(pscp.exe所在目录)2:上传(主要利用pscp程序)pscp d:/jdk-8u ...

  6. (hdu)5546 Ancient Go

    Problem Description Yu Zhou likes to play Go with Su Lu. From the historical research, we found that ...

  7. C++类继承内存布局(三)

    参考:http://blog.csdn.net/jiangyi711/article/details/4890889# (三)成员函数 类X中每一个非静态成员函数都会接受一个特殊的隐藏参数——this ...

  8. ASP.NET前端语法应用

    字符拼接 <%# "abc" + Eval("列名").ToString() %> <%# Eval("列名"," ...

  9. mysql 查找包含特定名字的表

    SELECT distinct TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME LIKE '%medias%'

  10. [C#]Base使用小记

    base 关键字用于从派生类中访问基类的成员: • 调用基类上已被其他方法重写的方法. • 指定创建派生类实例时应调用的基类构造函数. 基类访问只能在构造函数.实例方法或实例属性访问器中进行. 从静态 ...